vandium-io / lov Goto Github PK
View Code? Open in Web Editor NEWLightweight Object Validator (LOV)
License: BSD 3-Clause "New" or "Revised" License
Lightweight Object Validator (LOV)
License: BSD 3-Clause "New" or "Revised" License
when validating against a schema that has keys that are not required, if you don't enter in values for the required keys, they will be autofilled to undefined by default
'use strict';
const lov = require( 'lov' );
let schema = {
name: lov.string(),
lucky_number: lov.number(),
favorite_video_game: lov.array()
};
let value = {};
let validatedValue = lov.validate( value, schema );
/*
NOTE: will get same results with,
value = null;
OR
value = undefined;
*/
console.log( validatedValue );
/*
logs:
{ error: null,
value:
{ name: undefined,
lucky_number: undefined,
favorite_video_game: undefined } }
*/
It might be better if the values are not autofilled in this case
For example, the following code:
const lov = require( 'lov' );
const arr = [ 'a', 'b', 'c' ];
const schema = lov.array().required();
lov.validate( arr, schema );
throws an error. But it would be useful if you could validate schemas for individual values that are not within objects. Such are arrays, strings, or numbers.
Lov currently allows an object to be validated using the string validator.
const lov = require( 'lov' );
let not_a_string = {key:'value'};
let stringSchema = lov.string().required();
let result = lov.validate( not_a_string, stringSchema );
//result.error is null
//result.value is '[object Object]'
Using Joi, the same validation gives an error:
const Joi = require( 'Joi' );
let not_a_string = {key:'value'};
let stringSchema = Joi.string().required();
let result = Joi.validate( not_a_string, stringSchema );
/*
result.error is:
{ [ValidationError: "value" must be a string]
isJoi: true,
name: 'ValidationError',
details:
[ { message: '"value" must be a string',
path: 'value',
type: 'string.base',
context: [Object] } ],
_object: { key: 'value' },
annotate: [Function] }
result.value is: { key: 'value' }
typeof result.value === 'object'
*/
Similar for numbers:
const lov = require( 'lov' );
let not_a_string = 22;
let stringSchema = lov.string().required();
let result = lov.validate( not_a_string, stringSchema );
//result.error is null
//result.value is '22'
and Joi:
const Joi = require( 'Joi' );
let not_a_string = 22;
let stringSchema = Joi.string().required();
let result = Joi.validate( not_a_string, stringSchema );
/*
result.error is:
{ [ValidationError: "value" must be a string]
isJoi: true,
name: 'ValidationError',
details:
[ { message: '"value" must be a string',
path: 'value',
type: 'string.base',
context: [Object] } ],
_object: 22,
annotate: [Function] }
result.value is: 22
typeof result.value === 'number'
*/
I think making the validation more strict could improve Lov.
How can I validate at least one of the following?
["cloudy" "sunny", "rainy"]
Thanks
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.