1.0.2 • Published 8 years ago

relax-schema v1.0.2

Weekly downloads
39
License
Apache-2.0
Repository
github
Last release
8 years ago

npm version Build Status Coverage Status Dependency Status

relax-schema

Schema

Structured data with declarative validation

Kind: global class

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
  }
})
ParamTypeDescription
namestringThe name of the model.
constraintsObjectThe constraints this model is subject to.
validatorsObjectCustom 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:

ParamTypeDescription
docObjectThe document being validated.

ValidationError

Kind: global class

new ValidationError(result, doc, constraints)

Create a validation-specific error.

ParamTypeDescription
resultArray.<Object>Validation errors.
docObjectThe document that did not validate.
constraintsObjectThe configured validation rules.