tcomb-validator-plus
v1.3.9
Published
tcomb-validator-plus ====================
Downloads
9
Readme
tcomb-validator-plus
Advanced validation helpers for tcomb-validation
Features
- Used validator.js for strings
- Custom error messages
- Context binding
- Compile from simple schema
Extending rules
import createValidator from 'tcomb-validator-plus'
import extend from 'tcomb-validator-plus/extend'
import gt from 'tcomb-validator-plus/getType'
import {intersection} from 'tcomb'
function fakeLocalize(message, context) {
return {
message,
context
}
}
const validate = createValidator(fakeLocalize)
const password = (min, max) => intersection([
gt('isLengthMin')(min),
gt('isLengthMax', 'password greate than #{val}')(max)
])
extend('password', password)
const schema = struct({
password: gt('password')(3, 5),
})
const compiledSchema = compile({
password: 'password'
})
const data = {
password: '12'
}
const errors = {
errors: {
password: {
context: {
actual: '12',
val: 3
},
message: 'isLengthMin'
}
},
isError: true,
params: {
password: '12'
}
}
const obj = validate(schema)(data)
assert.deepEqual(obj, errors)
const obj2 = validate(compiledSchema)(data)
assert.deepEqual(obj2, errors)
Simple usage
// simple-1.js
import assert from 'power-assert'
import createValidator from 'tcomb-validator-plus'
import compile from 'tcomb-validator-plus/compile'
function fakeLocalize(message, context) {
return {
message,
context
}
}
const validate = createValidator(fakeLocalize)
const myValidate = validate(compile({
confirmed: {v: 'required', l: 'check confirm'},
password: [
['isLengthMin', 3],
{l: 'password greate than #{val}', v: ['isLengthMax', 5]}
],
confirmPassword: [
['isLengthMin', 3],
{l: 'password greate than #{val}', v: ['isLengthMax', 5]}
],
user: {v: 'required', l: 'user required'},
opts: {v: ['listOf', ['opt1', 'opt2']], l: '#{actual} is not a one of #{val}'},
__: {v: ['equal', 'password', 'confirmPassword'], l: 'Passwords are not equal'}
}))
const data = {
confirmed: false,
password: '1234',
confirmPassword: '1235',
user: '',
opts: ['opt1', 'opt3']
}
const errors = {
errors: {
confirmed: {
message: 'check confirm',
context: {
actual: false
}
},
user: {
message: 'user required',
context: {
actual: ''
path: ['user'],
}
},
opts: {
message: '#{actual} is not a one of #{val}',
context: {
val: 'opt1, opt2',
actual: 'opt3'
}
}
},
isError: true,
params: {
confirmed: false,
password: '1234',
confirmPassword: '1235',
user: '',
opts: ['opt1', 'opt3']
}
}
const obj1 = myValidate(data)
assert.deepEqual(obj1, errors)
const data2 = {
confirmed: true,
password: '1234',
confirmPassword: '1235',
user: 'test',
opts: ['opt1', 'opt2']
}
const errors = {
errors: {
__: {
context: {
from: 'password',
to: 'confirmPassword',
actual: {
confirmed: true,
password: '1234',
confirmPassword: '1235',
user: 'test',
opts: [
'opt1',
'opt2'
]
}
},
message: 'Passwords are not equal'
}
},
isError: true,
{
confirmed: true,
password: '1234',
confirmPassword: '1235',
user: 'test',
opts: ['opt1', 'opt2']
}
}
const obj2 = myValidate(data2)
assert.deepEqual(obj2, errors)
Promise usage
import {validate} from 'tcomb-validation'
import assert from 'power-assert'
import createValidator from 'tcomb-validator-plus'
import promisifyValidate, {ParamsError} from 'tcomb-validator-plus/promisifyValidate'
import compile from 'tcomb-validator-plus/compile'
function fakeLocalize(message, context) {
return {
message,
context
}
}
const validate = promisifyValidate(createValidator(fakeLocalize))
const myValidate = validate(compile({
confirmed: {v: 'required', l: 'check confirm'},
password: [
['isLengthMin', 3],
{l: 'password greate than #{val}', v: ['isLengthMax', 5]}
],
confirmPassword: [
['isLengthMin', 3],
{l: 'password greate than #{val}', v: ['isLengthMax', 5]}
],
user: {v: 'required', l: 'user required'},
opts: {v: ['listOf', ['opt1', 'opt2']], l: '#{actual} is not a one of #{val}'},
__: {v: ['equal', 'password', 'confirmPassword'], l: 'Passwords are not equal'}
}))
const data = {
confirmed: false,
password: '1234',
confirmPassword: '1235',
user: '',
opts: ['opt1', 'opt3']
}
const errors = {
errors: {
confirmed: {
message: 'check confirm',
context: {
actual: false
}
},
user: {
message: 'user required',
context: {
actual: ''
path: ['user'],
}
},
opts: {
message: '#{actual} is not a one of #{val}',
context: {
val: 'opt1, opt2',
actual: 'opt3'
}
}
},
isError: true,
params: {
confirmed: false,
password: '1234',
confirmPassword: '1235',
user: '',
opts: ['opt1', 'opt3']
}
}
myValidate(data)
.then(params => {
assert.deepEqual(params, data)
})
.catch(e => {
assert.deepEqual(e.errors, errors)
})