0.0.1 • Published 8 years ago

plius v0.0.1

Weekly downloads
2
License
MIT
Repository
-
Last release
8 years ago

Plius for Node

:warning: We are working hard on an alpha release. Below is what it will be able to do.

This is the JavaScript implementation of Plius, a language-agnostic document schema engine.

$ npm i --save plius

Plius uses generators to generate only the validation messages your application needs. This saves both in terms of logic to maintain and in performance.

Consider the following schema for validating a user:

userschema.json

{
  "type": "Map",
  "keys": {
    "email": {
      "type": "String",
      "regex": "\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}\b",
      "required": true
    },
    "name": {
      "type": "String",
      "minLength": 5
    },
    "password": {
      "minLength": 7
      "required": true
    }
  }
}

Note that the above schema can be used with any other Plius engine.

import { SchemaEngine, Validator } from "plius"

const engine = new SchemaEngine()

const validator = new Validator(engine.compile(
    JSON.parse(fs.readFileSync(__dirname+'/userschema.json'))))

const violations = validator.getViolations({ 
  name: "bob",
  email: "bob@example",
  password: "bobiscool"
})

console.log('Wrong fields:') 
for (const violation of violations)
  console.log(violation.path)

Results in the following output:

Wrong fields:
email
name

Which, using the translation engine, can be prettified to:

[{
  path: "email"
, message: "Input did not match pattern."
}, {
  path: "name",
, "message": "Minimum length of 5 characters required."
}]

API

import { SchemaEngine } from "plius"

new SchemaEngine(options)

  • useDefault: whether to add the default constraints and types, such as String, Boolean and Float.