pearlshare / hannibal Goto Github PK
View Code? Open in Web Editor NEWValidate JavaScript objects based on plan, useful for defining APIs and interfaces.
Validate JavaScript objects based on plan, useful for defining APIs and interfaces.
Suggested API for renaming keys
"rename": {
"foo.bar": "foo.baz"
}
foo.bar
is the path in the object so for example the following
{
foo: {
bar: 3
}
}
Would end up as
{
foo: {
baz: 3
}
}
Example:
var simpleValidator = hannibal.create({
type: 'object',
schema: {
shouldBeNumber: {
type: 'number',
transforms: 'toFloat'
}
}
});
simpleValidator({
shouldBeNumber: 'notAnumber'
}).isValid; //returns true
Date fails too:
var simpleValidator2 = hannibal.create({
type: 'object',
schema: {
shouldBeDate: {
type: 'date',
transforms: 'toDate'
}
}
});
simpleValidator2({
shouldBeDate: 'notAdate'
}).isValid; //true again
Currently transforms have a signature of (value, opts)
.
Suggest we pass two extra args so it's (value, opts, wholeObject, path)
. Where:
value
= the value being validatedopts
= options passed to the transformwholeObject
= the entire object (from root level) being validatedpath
= a lodash.get style object path such as items[0].url
This means if you have a custom transform or validator which depends on another value in the object it can be obtained.
For example, with the object:
{
type: "fish",
payload: {
name: "Nemo",
fins: 2
}
}
The payload may want to be validated differently based on the type.
{
type: "marsupial",
payload: {
name: "Skippy",
pouchDepth: 43
}
}
See https://runkit.com/eliothowes1090/hannibal-error-example for example of issue.
Given the following schema:
{
type: 'object',
schema: {
foo: {
type: 'array',
validators: {
max: 2
},
schema: {
type: 'string',
validators: {
enum: [
'fish',
'chips'
]
}
}
}
}
}
And the following data structure:
{
foo: [
'',
'not fish',
'not chips'
]
}
The following error object is returned:
foo: [
{enum: "string: is not one of: fish, chips"},
{enum: "string: not chips is not one of: fish, chips"},
{enum: "string: not chips is not one of: fish, chips"},
max: "array is too long, requires: 2 items and was: 3"
]
If a schema is given such as:
{
payload: {
type: "object",
schemas: [
// Schema for a link with number of clicks
{
url: {
type: "string"
},
clickCount: {
type: "number"
}
},
// schema for a user
{
firstName: {
type: "string"
},
lastName: {
type: "string"
}
}
]
}
}
Then it will allow items through which pass one or more schemas.
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.