npm.io
1.0.2 • Published 10 years ago

relax-schema

Licence
Apache-2.0
Version
1.0.2
Deps
3
Vulns
1
Weekly
0
DeprecatedThis package is deprecated

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
  }
})
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.

schema.validateSync(doc) ⇒ Object

Ensure that a given document can meet this model's constraints.

Kind: instance method of Schema
Returns: Object - 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.

Keywords