@kegovc/schema2joi
v1.0.7
Published
**A layer between Joi and your implementation in order to have a dynamic validation schema** ## Installation
Downloads
4
Readme
@kegovc/schema2joi
A layer between Joi and your implementation in order to have a dynamic validation schema
Installation
npm install schema2joi
#####or
yarn add schema2joi
Example
schema2joi allows you to describe your data using a simple language based on json schemas, to pass it to a schema-joi. schema2joi uses all the benefits of the joi object using the waterfall paradigm.
Example
const schema2joi = require('@kegovc/schema2joi')
const schema = schema2joi({
username:{
type: 'alphanum',
min: 3,
max: 30,
required: true,
},
password:{
type: 'string',
pattern: /^[a-zA-Z0-9]{3,30}$/
}
repeat_password: {
ref: 'password'
}
access_token: [
{
type: 'string'
},
{
type: 'number'
},
],
birth_year:{
type: 'number',
format: 'integer',
min: 1900,
max: 2013,
},
email: {
type: 'string',
format: {
fun:'email',
param:{
minDomainSegments: 2,
tlds: {
allow: ['com', 'net']
}
}
},
},
extras: {
type: 'object',
properties: {
val: {
type: 'string'
},
val2: {
type: 'number',
required: true,
}
}
}
})
.with('username', 'birth_year')
.xor('password', 'access_token')
.with('password', 'repeat_password');
const val1 = schema.validate({ username: 'abc', birth_year: 1994 });
console.log(val1.error)
// -> { value: { username: 'abc', birth_year: 1994 } }
const val2 = schema.validate({});
console.log(val2.error)
// -> { value: {}, error: '"username" is required' }
// Also -
try {
const valueAsync = await schema.validateAsync({
username: 'abc',
birth_year: 1994
});
}
catch (err) {
console.log('errorAsync', err)
}