Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

segv/jsoSchema

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

90 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jsoSchema

jsoSchema is an engine, and a kind of schema language, for validating javascript objects (often, but not always, json data sent between a browser and a web server).

Note that jsoSchema is not a JSON text parser/validator. It operates on in-memory objects only, and its schema is not just data (objects with a static, and serializable, stata ala JSON Schema and XSD) actual code (functions). While a jsoSchema can be used to generate a json schema (or any other kind of schema), since it’s "just" javascript objects and methods, it can not be parsed and minpulated by something other than jsoSchema.

Synopsis

// load the module
var s = require('jsoSchema');

// create a schema which matches either a positive integer or a string
var schema = s.Or(s.GreaterThan(0),
                  s.String());

// assuming foo holds our value, test it agains the schema
var test = schema.test(foo)

// the returned object has, among other things, a boolean property
// called match. this property is, surprisingly enough, true if the
// object matched the schema and false otherwise.
if (test.match) {
   console.log('foo is a positive integer or a string');
} else {
   console.log('foo is not a positive integer nor a string');
   console.log('failed because:', test.backtrace());
}

Reference

See API.

Installation

with npm

npm install jsoschema

without npm

How many ways are there to install, compile and deploy js code? too many; and I have no desire to try and keep up with them all. Considering that, the code itself is exactly one file of javascript:

It should work in nodejs/npm (that’s the environment the code is developed and tested in) directly and, i would assume but haven’t tested, in the browser as well.

If it doesn’t, or if you’re just curious: At the bottom of that file is a single function, bound to the exporter variable in the jsoSchema code itself, which takes as input the jsoSchema object and does whatever is needed to export the object. Please feel free to edit that bit of code to suite your needs.

Compared to JSON schema

While jsoSchema and JSON Schema server very similar purposes, they go about it in two very different ways. A JSON Schema is a bit of data which is passed to a validator, along with the actual data to validate, and the validator understands the semantics of JSON Schema and dos what the schema says it should do given the data it has.

a jsoSchema is a block of code describing how to test if something is valid or not.

This code vs data approach has two important consequences:

  1. A JSON Schema can be represented, and transmitted and stored, as JSON data; a jsoSchema can not.

  2. A jsoSchema can perfrom any computation that’s needed; a JSON Schema is limited to the constraints defined in the JSON Schema specification.

About

Javascript Object Schema

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published