1.0.0 • Published 2 years ago
@solvethex/validator v1.0.0
Use the Fastest Validator library to validate schemas.
import validator from 'fastestValidatorService'
const schema = {
name: 'string',
age: 'string|numeric'
}
// Validate the schema with a default validator (English by default)
const valid = validator.validate({name: 'victor', age: '20'}, schema) // valid => { success: true }
// Add messages for a language
validator.setMessages({
en: {
required: 'This field is required!'
}
})
// Now, you can select a previous added language to use the given messages for errors
const result = validator.validate({name: 'victor'}, schema, { lang: 'en' })
/*
result => {
success: false,
errors: [
{
actual: undefined,
field: 'age',
message: 'The field is required!',
type: 'required'
},
]
}
*/
// Also, you can ask the validator to cache your schema with a key
// If the key already exists, the validator will use the previous compiled
// schema instead of the schema you provided
const cachedResult = validator.validate({name: 'victor', age: '20'}, schema, { cache: 'mySchema' })
// You can clear the cache also if you want
validator.clearCache()
// Have fun!
1.0.0
2 years ago