valydet
v1.0.0
Published
- A tiny module to validate JSON data.
Downloads
4
Maintainers
Readme
Valydet
- A tiny module to validate JSON data.
Status
Usage
const valydet = require('valydet');
const PostSchema = new valydet.Schema({
id: { type: 'string', required: true },
title: { type: 'string', required: true },
by: { type: 'email', required: true }
});
// Validate data.
let result = PostSchema.validate({
id: 'testId',
title: '',
by: 'invalidemail'
});
// access errors list
console.log(result.failures());
API
new valydet.Schema(schemaDefinition)
- Generates a new schema for the given schema definition
- Params
- schemaDefinition - Schema Definition containing the rules.
- Uses is.js for validation each attribute.
- schemaDefinition - Schema Definition containing the rules.
Usage:
const valydet = require('valydet');
const SomeSchema = new valydet.Schema({
id: { type: 'string',
required: true,
// You can have custom error message to override
// the default message.
message: 'Custom error message'
},
email: { type: 'email': required: true }
});
schemaInstance.validate(data)
Validates the supplied data against the schema definition.
returns: [] of errors
schemaInstance.failures()
- returns: list of errors
// error format
{
key: 'id' // property name.,
message: 'Something went wrong' // Error message.
}