@brainhubeu/hadron-validation
v1.0.0
Published
Hadron validation layer module
Downloads
53
Readme
Installation
npm install @brainhubeu/hadron-validation --save
Creating schema files
To use validation layer, first you need to provide some schemas. We use JSON Schema format, for example:
{
"type": "object",
"properties": {
"id": {
"type": "number"
},
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"required": ["name", "age"],
"additionalProperties": false
}
Full documentation about JSON Schema: Ajv documentation
Preparing schema files for usage with Hadron
When your schemas are ready you need to build object from them, where key name is a name of the schema, for example:
// schemas.js
const insertTeam = require('./team/insertTeam.json');
const updateTeam = require('./team/updateTeam.json');
const insertUser = require('./user/insertUser.json');
const updateUser = require('./user/updateUser.json');
const schemas = {
insertTeam,
updateTeam,
insertUser,
updateUser,
};
module.exports = schemas;
Validate function
After you create schemas object you can create validate function from Hadron validator factory
// validate.js
const validatorFactory = require('@brainhubeu/hadron-validation');
const schemas = require('./schemas');
module.exports = validatorFactory(schemas);
Using validate function
After you created validate function with hadron validator factory you can use it to validate object by schemas you provide.
const validObject = {
name: 'Max',
age: 22,
};
validate('schemaName', objectToValidate)
.then((validObject) => {
console.log('I am a valid object', validObject);
})
.catch((error) => {
console.log('Object is invalid', error);
});
Validate function passes valid object, otherwise it throws an error.