Giter Club home page Giter Club logo

tv4's Introduction

#Tiny Validator (for v4 JSON Schema)

All you need is tv4.js (19.7KB) or tv4.min.js (11.8KB, 3.51KB gzipped).

There is support for $ref with JSON Pointer fragment paths (other-schema.json#/properties/myKey).

Usage

var valid = tv4.validate(data, schema);

If validation returns false, then an explanation of why validation failed can be found in tv4.error.

The error object will look something like:

{
    "code": 0,
    "message": "Invalid type: string",
    "dataPath": "/intKey",
    "schemaKey": "/properties/intKey/type"
}

The "code" property will refer to one of the values in tv4.errorCodes - in this case, tv4.errorCodes.INVALID_TYPE.

To enable external schema to be referenced, you use:

tv4.addSchema(url, schema);

If schemas are referenced ($ref) but not known, then validation will return true and the missing schema(s) will be listed in tv4.missing.

Multi-threaded validation

Storing the error and missing schemas does not work well in multi-threaded environments, so there is an alternative syntax:

var result = tv4.validateResult(data, schema);

The result will look something like:

{
    "valid": false,
    "error": {...},
    "missing": [...]
}

Asynchronous validation

Support for asynchronous validation (where missing schemas are fetched) can be added by including an extra JavaScript file. Currently, the only version requires jQuery (tv4.async-jquery.js), but the code is very short and should be fairly easy to modify for other libraries (such as MooTools).

Usage:

tv4.validate(data, schema, function (isValid, validationError) { ... });

validationFailure is simply taken from tv4.error.

Tests

There are tests available, but they require PHP, so you can't see them on GitHub.

Minifying

tv4.min.js is produced using the Google Closure Compiler.

License

The code is available as "public domain", meaning that it is completely free to use, without any restrictions at all. Read the full license here.

It's also available under an (MIT license](http://jsonary.com/LICENSE.txt).

tv4's People

Contributors

geraintluff avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.