plius

A schema engine for semi-structured documents written in pure JavaScript

Project deprecated in favour of JSON schemas. If you want the package name, please contact NPM.
Last updated 4 years ago by samvv .
MIT · Repository · Original npm · Tarball · package.json
$ cnpm install plius 
SYNC missed versions from official npm registry.

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.

Current Tags

  • 0.0.1                                ...           latest (4 years ago)

1 Versions

  • 0.0.1 [deprecated]           ...           4 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 3
Dependencies (3)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2016 © taobao.org |