A data storage and manipulation library for javascript
Schema enforcement.
Param | Type |
---|---|
schema | SchemaDefinition |
Example
import { Schema } from 'hord';
const person = new Schema({
first: String,
last: String,
age: 'integer',
hobbies: {
type: Array,
content: String
}
});
person.validate({
first: 'John',
last: 'Doe',
age: 21
});
// => []
schema.validate(item, [path]) ⇒ Array.<SchemaError>
Validate an item against the schema.
Param | Type | Default | Description |
---|---|---|---|
item | object |
The object validate against this schema. | |
[path] | Array |
[] |
If provided then only the value at that path will be validated. |
schema.enforce(item, [path], [replace]) ⇒ Array.<SchemaError>
Enforce an items structure against the schema. This function mutates the original item.
Param | Type | Default | Description |
---|---|---|---|
item | object |
The object enforce against this schema. | |
[path] | Array |
[] |
If provided then only the value at that path will be enforced. |
[replace] | unknown |
If the current value at path is invalid, replace it with this. |
Calls a callback for each rule that will be used to validate this schema.
Param | Type | Description |
---|---|---|
callback | function |
Provides two args: the path and the rule. If true is returned then no more callbacks will happen further down this branch, but will continue up a level. |
schema.extend(schema) ⇒ Schema
Returns a new Schema with the rules from the provided schema superimposed on the rules from this schema. If no args are provided, then the returned Schema is effectively a clone of this one.
Param | Type | Description |
---|---|---|
schema | SchemaDefinition , Schema |
The schema to superimpose on this one. |
Schema validation errors
Param | Type | Description |
---|---|---|
error | string |
A message about the type of error |
path | string |
The path within the given item to the value causing the error |
value | unknown |
The value at this path |
item | unknown |
The original item being validated |
Schema type definitions. Can be just the type as defined below, or an array of types, or an object with the following options. Any extra options provided will be copied to the rule, which can be accessed via the schema.eachRule() method.
'*' can be used as a key to indicate that any keys are allowed in an object.
Properties
Name | Type | Default | Description |
---|---|---|---|
type | * | Array |
Supported native types are Array, Boolean, Date, Element, Function, Number, Object, RegExp, String. Also supports '*', 'integer', 'float', Enum (from type-enforcer), custom constructors (classes or constructor functions), or instances of Schema or Model. | |
[isRequired] | boolean |
false |
Empty arrays or objects that aren't required will be removed by schema.enforce(). |
[default] | boolean |
If isRequired is true, then schema.enforce() will set this value if the key is undefined. | |
[coerce] | boolean |
false |
If true then values that can be coerced into the specified type will not return errors and will be coerced in schema.enforce(). |
[min] | number |
For Number, 'integer', and 'float' | |
[max] | number |
For Number, 'integer', and 'float' | |
[minLength] | number |
For Arrays and Strings | |
[maxLength] | number |
For Arrays and Strings | |
[clamp] | boolean |
false |
Works with min, max, minength, and maxLength. If true then values outside the range will be forced within the range. If false then values outside the range will be deleted. |
[enum] | Enum |
If type is Enum, then this is required | |
[content] | object | Array |
For arrays and objects to specify further content | |
[enforce] | function |
This is automatically included, but can be overridden. (See type-enforcer enforce for more info) | |
[check] | function |
This is automatically included, but can be overridden. (See type-enforcer checks for more info) |
Example
import { Schema } from 'hord';
// Can be a native type or string
const person = new Schema({
first: String,
last: String,
age: 'integer'
});
// Or with options:
const person = new Schema({
first: {
type: String,
isRequired: true
},
last: {
type: String,
isRequired: true
},
age: {
type: 'integer'
min: 0,
coerce: true
}
});