百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
J

jjv

> 编程语言
开源

JavaScript 方案验证库

200 stars0 点赞3 次浏览
访问官网GitHub

工具介绍

JavaScript 方案验证库

JJV: JJV JSON Validator

A simple and extensible json-schema validator written in javascript. JJV runs in the browser and in the server (through node.js), it has no dependencies and has out-of-the-box AMD support.

JJV implements the latest (v4) JSON Schema Core draft, however due to performance and security concerns remote schemas are not fetched. To ensure compliance JJV is tested against JSON Schema Test Suite published by json-schema.org (and passes all tests). For examples and a detailed description of the JSON-schema specification visit json-schema.org.

JJV is fast! For a detailed performance comparison visit z-schema's benchmarks website, which compares various javascript JSON schema validators.

Basic Usage

In the most basic usage an environment must be created, and one or more named schemas are registered in the environment (it is also possible to register schemas with remote URI's in the same way). Javascript objects can then be validated against any registered schema.

…

It is also possible to validate objects against unregistered and/or unnamed schemas by supplying the schema object directly. For example:

javascript
var env = jjv();

var errors = env.validate({
    type: 'object',
    properties: {
        x: {
            type: 'number'
        },
        y: {
            type: 'number'
        }
    },
    required: ['x', 'y']
 }, {x: 20, y: 50});

Validation Options

JJV provides options to control the validation of required fields, the handling of default values, and the handling of additional properties.

    Option
    Default
    Description




    checkRequired
    true
    If true it reports missing required properties, otherwise it
    allows missing required properties.




    useDefault
    false
    If true it modifies the object to have the default values for
    missing non-required fields.




    useCoerce
    false
    If true it enables type coercion where defined.




    removeAdditional
    false
    If true it removes all attributes of an object which are not
    matched by the schema's specification.

The defaults can be overridden for the entire environment or on a per-validation basis. For example, to override the checkRequired option for the entire environment simply do:

javascript
env.defaultOptions.checkRequired=false;

To override the checkRequired option on a per-validation case do:

env.validate('schemaName', object, {checkRequired: false});

Advanced Usage

JJV provides mechanisms to add support for custom types, custom formats, and custom checks.

Custom Types

Support for additional types can be added through the addType function. For example, a simple implementation of the date type could be the following:

javascript
env.addType('date', function (v) {
  return !isNaN(Date.parse(v));
});

Custom Formats

It is also possible to add support for additional string formats through the addFormat function. For example, an implementation of the hexadecimal string format (already included) could be as follows:

javascript
env.addFormat('hexadecimal', function (v) {
    return (/^[a-fA-F0-9]+$/).test(v);
});

Custom Checks

It is possible to add support for custom checks (i.e., minItems, maxItems, minLength, maxLength, etc.) through the addCheck function. For example, an implementation for an exactLength validation keyword that supports arrays and strings can be achieved with the following:

javascript
env.addCheck('exactLength', function (v, p) {
    return v.length === p;
});

Custom Type Coercion

JJV allows custom type coercion rules. As an example, supposed that fields which are declared with as type integer are sometimes encoded as a string. Type coercion allows you to specify that all types declared as integer should be cast/coerced to an integer before performing validation.

javascript
env.addTypeCoercion('integer', function (x) {
    return parseInt(x, 10);
});

Recall to set the option useCoerce to true to enable this feature.

$data v5 proposal

JJV supports the $data spec proposed for draft 5 of json-schema, complete with relative and absolute JSON pointers.

For information on how to use these feature see the proposal here:

$data-proposal.

Issues· 18 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScript

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言