- Source:
Namespaces
- ERROR_KEYS
- ERROR_MESSAGES
- spec
- validators/any
- validators/array
- validators/boolean
- validators/number
- validators/shape
- validators/string
Methods
(static) createValidateFunction(opts) → {Array.<function()>}
Function helps to create your own validation functions and define your own validation messages
Parameters:
| Name | Type | Description | |||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|
| opts | Object | object with  Properties
 | 
Returns:
tuple with two functions e.g: [validate, validateWithOpts]
- Type
- Array.<function()>
- Source:
Example
import {
 spec,
 createValidateFunction,
 toValidator,
 toValidatorWithArgs,
 ERROR_KEYS,
 ERROR_MESSAGES,
} from 'egoist-js'
const customeErrorKey = 'my.custom.error.key'
const customMessageFunc = {
  ...ERROR_MESSAGES,
  [ERROR_KEYS.ANY.REQUIRED]: ({ label, args, value, path }) => `${label} is mandatory`,
  [customeErrorKey]: ({ label, args, value, path }) => `${label} bla bla bla`,
}
const [cvalidate, cvalidateWithOpts] = createValidateFunction({
 untilFail: true,
 messageFuncs: customMessageFunc,
})
const [cvalidateAll, cvalidateAllWithOpts] = createValidateFunction({
 untilFail: true,
 messageFuncs: customMessageFunc,
})
const customValidator = toValidator(customeErrorKey, (value) => Boolean(value))
console.log(cvalidate(spec.flow(customValidator), 0))
// [{ messsge: 'value bla bla bla', value: 0, args: undefined, path: [] }](static) isInvalid(specFn, value) → {boolean}
Curried function tells whether value is invalid
Parameters:
| Name | Type | Description | 
|---|---|---|
| specFn | function | |
| value | any | value to check | 
Returns:
True if value is invalid, othervise false
- Type
- boolean
- Source:
Example
import { isInvalid, spec, validators } from 'egoist-js'
const { string, any } = validators
const usernameSpec = spec.flow(string.isString, string.isNotEmpty, any.required)
console.log(isInvalid(usernameSpec, 'hello')) // false
const isInvalidUsernameFn = isInvalid(usernameSpec)
console.log(isInvalidUsernameFn('nope')) // false
console.log(isInvalidUsernameFn(undefined)) // true
console.log(isInvalidUsernameFn(null)) // true
console.log(isInvalidUsernameFn('')) // true(static) isValid(specFn, value) → {boolean}
Curried function tells whether value is valid
Parameters:
| Name | Type | Description | 
|---|---|---|
| specFn | function | |
| value | any | value to check | 
Returns:
True if value is valid, othervise false
- Type
- boolean
- Source:
Example
import { isValid, spec, validators } from 'egoist-js'
const { string, any } = validators
const usernameSpec = spec.flow(string.isString, string.isNotEmpty, any.required)
console.log(isValid(usernameSpec, 'hello')) // true
const isValidUsernameFn = isValid(usernameSpec)
console.log(isValidUsernameFn('nope')) // true
console.log(isValidUsernameFn(undefined)) // false
console.log(isValidUsernameFn(null)) // false
console.log(isValidUsernameFn('')) // false(static) validate(specFn, value) → {Array.<Object>|Null}
Curried function returns validation info if data has errors
Parameters:
| Name | Type | Description | 
|---|---|---|
| specFn | function | |
| value | any | value to check | 
Returns:
Null if data is valid, otherwise one item array with reason of error
- Type
- Array.<Object> | Null
- Source:
Example
import { validate, spec, validators } from 'egoist-js'
const { shape, number, string, any } = validators
const usernameSpec = spec.flow(string.isString, string.isNotEmpty, any.required)
console.log(validate(usernameSpec, 'hello')) // null
const validateUsername = validate(usernameSpec)
console.log(validateUsername(null)) 
// [{
//  message: 'value is required', 
//  value: null, 
//  args: undefined, 
//  path: [] // path is computed property
// }]
const adultUserModelSpec = spec.compose(
 spec.flow(
  shape.isShape,
  shape.hasKeys('name', 'age'),
  any.required,
 ),
 spec.of({
  name: spec.flow(string.isString, string.isNotEmpty, any.required),
  age: spec.flow(number.isNumber, number.ge(18), any.required),
 })
)
const validateAdultUser = validate(adultUserModelSpec)
const validationResult = validateAdultUser({
 name: 'Oliver Darkside',
 age: 17,
})
console.log(validationResult) 
// [{ message: 'age should be greater or equal than 18', value: 17, args: 18, path: ['age'] }](static) validateAll(specFn, value) → {Array.<Object>|Null}
Curried function returns info about all errors if data has them
Parameters:
| Name | Type | Description | 
|---|---|---|
| specFn | function | |
| value | any | value to check | 
Returns:
Null if data is valid, otherwise array with all reasons of error
- Type
- Array.<Object> | Null
- Source:
Example
import { validateAll, spec, validators } from 'egoist-js'
const { shape, number, string, any } = validators
const simpleSpec = spec.flow(
 string.isString,
 string.isDigits,
 string.isNotEmpty,
 any.required
)
console.log(validateAll(simpleSpec, 'hello'))
// [
//   { message: 'value should be a string', value: 'hello', args: undefined, path: [] },
//   { message: 'value should contain only digits', value: 'hello', args: undefined, path: [] },
// ](static) validateAllWithOpts(specFn, opts, value) → {Array.<Object>|Null}
Curried function with opts, returns info about all errors if data has them
Parameters:
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
| specFn | function | |||||||
| opts | Object | options object Properties
 | ||||||
| value | any | value to check | 
Returns:
Null if data is valid, otherwise one item array with reason of error
- Type
- Array.<Object> | Null
- Source:
Example
import { validateWithOpts, spec, validators, ERROR_KEYS } from 'egoist-js'
const { shape, number, string, any } = validators
const simpleSpec = spec.flow(
 string.isString,
 string.isDigits,
 string.isNotEmpty,
 any.required
)
const extValidateAll = validateAll(simpleSpec, {
 customMessages: {
  [ERROR_KEYS.STRING.TYPE]: ({ label, value, args, path }) => 'yeah, yeah, yeah ${label} should be a string',
 },
})
console.log(extValidateAll(simpleSpec, 'hello'))
// [
//   { message: 'yeah, yeah, yeah value should be a string', value: 'hello', args: undefined, path: [] },
//   { message: 'value should contain only digits', value: 'hello', args: undefined, path: [] },
// ](static) validateWithOpts(specFn, opts, value) → {Array.<Object>|Null}
Curried function with opts, returns validation info if data has errors
Parameters:
| Name | Type | Description | ||||||
|---|---|---|---|---|---|---|---|---|
| specFn | function | |||||||
| opts | Object | options object Properties
 | ||||||
| value | any | value to check | 
Returns:
Null if data is valid, otherwise one item array with reason of error
- Type
- Array.<Object> | Null
- Source:
Example
import { validateWithOpts, spec, validators, ERROR_KEYS } from 'egoist-js'
const { shape, number, string, any } = validators
const adultUserModelSpec = spec.of({
  name: spec.flow(string.isString, string.isNotEmpty, any.required),
  age: spec.flow(number.isNumber, number.ge(18), any.required),
  address: spec.of({
   city: spec.flow(string.isString, any.required),
   street: spec.flow(string.isString, any.required),
  })
})
const validateAdultUser = validateWithOpts(adultUserModelSpec, {
  customMessages: {
    [ERROR_KEYS.NUMBER.GE]: ({ label, path, value, args }) => 
       `${label} can not contain [${value}], because expects something greater or equal than ${args}`
  },
})
const reasons = validateAdultUser({
 name: 'Alice Morgan',
 age: 17,
 address: {
   city: 'London',
   street: 'Fitsrovia st.'
 }
})
console.log(reasons) 
// [{
//   message: 'age can not contain [17], because expects something greater or equal than 18',
//   args: 18,
//   value: 17,
//   path: ['age'],
// }]