- Source:
Methods
(static) compose() → {function}
Function is used to create a composition between specification functions
Parameters:
Name | Type | Description |
---|---|---|
...specs |
Array.<function()> |
Accpets spec functions as list of arguments |
Returns:
- Spec function which could be used as argument
- Type
- function
- Source:
Example
import { spec } from 'egoist-js'
const userModelBodySpec = spec.of({
username: spec.flow(string.isNotEmpty, any.required),
bestFriend: spec.lazy(() => userModelBodySpec),
})
const userModelRequiredSpec = spec.flow(any.requied)
const fullUserModelSpec = spec.compose(
userModelRequiredSpec,
spec.flow(shape.expectKeys(['username', 'bestFriend'])),
userModelBodySpec,
)
(static) designate(key, spec) → {function}
Usage of function makes sense only if u have spec flow outside of object context or collection context and want to rename output label for validation message
Parameters:
Name | Type | Description |
---|---|---|
key |
string |
key to use instead of 'value' for label to output validation message |
spec |
function |
Accpets spec function |
Returns:
- Spec function which could be used as argument for lib functions
- Type
- function
- Source:
Example
import { spec } from 'egoist-js'
const asUserSpec = spec.designate('user')
const userModelSpec = spec.compose(
asUserSpec(spec.flow(required)),
spec.of({
username: spec.flow(isNotEmpty, required),
friends: spec.of([spec.lazy(() => userModelSpec)]),
})
)
const result = validate(null, { untilFail: true })
// [{ message: 'user is required', value: null, args: undefined, path: [] }]
(static) flow() → {function}
Function is used to create validation flow
Parameters:
Name | Type | Description |
---|---|---|
...validators |
Array.<function()> |
Accepts validation functions as list of arguments |
Returns:
- Spec function which could be used as argument
- Type
- function
- Source:
Example
import { spec } from 'egoist-js'
const usernameSpec = spec.flow(
string.isString,
string.match(/^[A-Z][a-z]+\s[A-Z][a-z]+$/)
string.isNotMatch,
any.required
)
(static) lazy(getSpecLazy) → {LazySpec}
Function is used to create a lazy evaluate specification item
Parameters:
Name | Type | Description |
---|---|---|
getSpecLazy |
function |
Expected spec function to be passed |
Returns:
- Object which could be used as argument for #compose, #of functions
- Type
- LazySpec
- Source:
Example
import { spec } from 'egoist-js'
const userModelSpec = spec.of({
username: spec.flow(string.isNotEmpty, any.required),
bestFriend: spec.lazy(() => userModelSpec), // returns lazy evaluated spec function
})
(static) of(specDescriptor) → {function}
Function helps to create spec fof complex object such as arrays or shapes (aka objects)
Parameters:
Name | Type | Description |
---|---|---|
specDescriptor |
Object | Array |
Accepts object where properties' values must be spec functions or array with only one item that is a spec function |
Returns:
- Spec function which could be used as argument
- Type
- function
- Source:
Example
import { spec } from 'egoist-js'
const simpleHeroSpec = spec.of({
name: spec.flow(string.isString, string.isNotEmpty, any.required),
abilities: spec.of([spec.flow(any.required)])
address: spec.of({
city: spec.flow(any.required),
// pls note - spec can be complex
streets: spec.of([spec.of({ zip: spec.flow(any.required) })])
})
})