shallow-schema
v1.1.6
Published
A lightweight package for using simple JSON schemas.
Downloads
4
Readme
shallow-schema
A lightweight package for using simple JSON schemas.
Install
npm install shallow-schema
Usage
Requiring the module:
const shallowSchema = require('shallow-schema')
Testing a schema: testSchema
const { testSchema } = require('shallow-schema');
const s1 = { "name": { type: 'string' } };
const s2 = { "name": { type: 'strong' } };
console.log( testSchema(s1) ); // true
console.log( testSchema(s2) ); // false ('strong' is not a valid value for <type>)
Testing a source object: assertSchema
const { assertSchema } = require('shallow-schema');
const schema = { "name": { type: 'string' } };
const o1 = { name: 'alex' };
const o2 = { name: 9 };
const o3 = { Name: 'alex' };
console.log(assertSchema(o1)); // true
console.log(assertSchema(o2)); // false ('name' property is not of type 'string')
console.log(assertSchema(o3)); // false ('Name' property was not defined in schema)
API Reference:
testSchema:
testSchema(schema: Object): boolean
- Receives:
schema
- An object to test for schema validity
- Returns:
boolean
- Whether or not 'schema' parameter is a compliant schema
assertSchema:
assertSchema(schema: Object, source: Object): boolean
- Receives:
schema
- The schema to use as comparison basissource
- An object to compare
- Returns:
boolean
- Whether or not 'source' parameter matches the 'schema' configuration
- Exceptions:
- If the schema passed is not valid, and "Invalid Schema" error will be thrown.
Schema Construction Reference:
Structure:
A valid schema is an object containing one or more property declarations, containing the necessary fields.
const s = {
property_name: {
"type": string,
"optional": boolean,
"pattern": string,
"min": number
"max": number,
"values": array,
"test": function
};
};
Fields:
"type"
Type: String (one of: "string" | "function" | "array" | "number" | "any" | "boolean" | "object"
)
Omittable: No
This property is mandatory, and specifies the type of the property defined. It may be set to 'any' if the value of the property is variable.
"optional"
Type: Boolean
Omittable: Yes
Default: false
The 'optional' field can be set to true to indicate that the property in the scheme does not need to be defined in any compared objects.
"pattern"
Type: String Omittable: Yes
This generates different effects according to the property type:
string
: The whole string is compared.array
: All the array items are compared.object
: All the object values are compared.- Others: This option is ignored.
"min"
Type: Number Omittable: Yes
Compares the value of a property with this as minimum (inclusive).
This generates different effects according to the property type:
string
: The string length is compared.number
: The number value is compared.array
: The array length is compared.object
: The number of properties in the object is compared.- Others: This option is ignored.
"max"
Type: Number Omittable: Yes
Compares the value of a property with this as maximum (exclusive).
This generates different effects according to the property type:
string
: The string length is compared.number
: The number value is compared.array
: The array length is compared.object
: The number of properties in the object is compared.- Others: This option is ignored.
"values"
Type: Array Omittable: Yes
It is an array containing any number of items of any type. Checks if the value of the property matches one or more items in this array (case sensitive)
"test"
Type: Function Omittable: Yes
It is a callback function that receives a parameter "value". Checks if the function call with the property compared as argument returns true.
Example:
A schema that checks for an email, password and an optional age:
const s1 = {
email:{
type: 'string',
pattern: '^[a-zA-Z0-9.]+@[a-zA-Z0-9]+\.[a-zA-Z]+\.([a-zA-Z]+)?$'
},
password: {
type: 'string',
// Password min length is 8
min: '8',
// Password max length is 20
max: '20',
// Password must include a special char
test: (value) => (/\!\@\#\$\&\*/).test(value)
},
age: {
optional: true,
type: 'number',
// Age must be higher or equal 17 and lower than 100 (up to 99).
min: 1,
max: 100
}
}