validate-value
validate-value validates values against JSON schemas.
Installation
$ npm install validate-value
Quick start
First you need to integrate validate-value into your application:
const Value = require('validate-value');
Then, create a new instance and provide a JSON schema that you would like to use for validation:
const value = new Value({
type: 'object',
properties: {
username: { type: 'string' },
password: { type: 'string' }
},
additionalProperties: false,
required: [ 'username', 'password' ]
});
Afterwards, you may use the validate
function to validate a value:
const user = {
username: 'Jane Doe',
password: 'secret'
};
try {
value.validate(user);
} catch (ex) {
}
By default, the error message uses value
as identifier and .
as the separator for the object that is validated, but sometimes you may want to change this. Therefor, provide the desired identifier and separator as second parameter to the validate
function:
const user = {
username: 'Jane Doe',
password: 'secret'
};
try {
value.validate(user, { valueName: 'person', separator: '/' });
} catch (ex) {
}
From time to time, you may not be interested in the actual error, but only in the fact whether the given object is valid or not. For these cases, use the isValid
function:
const user = {
username: 'Jane Doe',
password: 'secret'
};
console.log(value.isValid(user));
Running the build
To build this module use roboter.
$ npx roboter
License
The MIT License (MIT)
Copyright (c) 2018-2019 the native web.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.