1.0.2 • Published 8 years ago
relax-schema v1.0.2
relax-schema
Schema
Structured data with declarative validation
Kind: global class
- Schema
- new Schema(name, constraints, validators)
- .validate(doc) ⇒ Promise.<Object>
new Schema(name, constraints, validators)
Create a structure for your data. Uses validate.js
to
provide declarative validation for your documents. Additional validation
functions may be specified by passing an object to the constructor.
Create a Schema using this simple declaration:
const BlogPost = new Schema("BlogPost", {
title: {
presence: true
},
datePublished: {
presence: true,
date: true
},
author: {
email: true
}
})
Param | Type | Description |
---|---|---|
name | string | The name of the model. |
constraints | Object | The constraints this model is subject to. |
validators | Object | Custom validation functions. |
schema.validate(doc) ⇒ Promise.<Object>
Ensure that a given document can meet this model's constraints.
Use a simple promise-based API to check that your data is correctly structured:
BlogPost.validate({
title: "My Adventure",
datePublished: "2012-04-20",
author: "jane@example.com"
}).then((doc) => {
// do something cool
}).catch((err) => {
// be strong
})
Kind: instance method of Schema
Returns: Promise.<Object> - A promise for the validated document.
Throws:
Param | Type | Description |
---|---|---|
doc | Object | The document being validated. |
ValidationError
Kind: global class
new ValidationError(result, doc, constraints)
Create a validation-specific error.
Param | Type | Description |
---|---|---|
result | Array.<Object> | Validation errors. |
doc | Object | The document that did not validate. |
constraints | Object | The configured validation rules. |