mafintosh/is-my-json-valid là một trong những repo mã nguồn mở mà TopGit theo dõi, hiện có 963 sao, viết chủ yếu bằng JavaScript. A JSONSchema validator that uses code generation to be extremely fast
Tóm tắt dựng từ metadata GitHub của chính dự án — chưa có bài review TopGit. Trang sẽ tự động cập nhật khi bài review đầy đủ được xuất bản.
VÌ SAO CHƯA CÓ REVIEW
TopGit viết bài đầy đủ cho repo có nhiều sao nhất và được yêu cầu nhiều nhất. Trang này là snapshot trong thời gian chờ — xem README gốc ở tab READ ME.
A JSONSchema validator that uses code generation to be extremely fast.
It passes the entire JSONSchema v4 test suite except for remoteRefs and maxLength/minLength when using unicode surrogate pairs.
Installation
npm install --save is-my-json-valid
Usage
Simply pass a schema to compile it
var validator = require('is-my-json-valid')
var validate = validator({
required: true,
type: 'object',
properties: {
hello: {
required: true,
type: 'string'
}
}
})
console.log('should be valid', validate({hello: 'world'}))
console.log('should not be valid', validate({}))
// get the last list of errors by checking validate.errors
// the following will print [{field: 'data.hello', message: 'is required'}]
console.log(validate.errors)
You can also pass the schema as a string
var validate = validator('{"type": ... }')
Optionally you can use the require submodule to load a schema from __dirname
var validator = require('is-my-json-valid/require')
var validate = validator('my-schema.json')
Custom formats
is-my-json-valid supports the formats specified in JSON schema v4 (such as date-time).
If you want to add your own custom formats pass them as the formats options to the validator
Many popular libraries make it easy to retrieve the failing rule with the schemaPath:
var schemaPath = validate.errors[0].schemaPath
var R = require('ramda')
console.log( 'All evaluate to the same thing: ', R.equals(
schema.properties.hello,
{ required: true, type: 'string' },
R.path(schemaPath, schema),
require('lodash').get(schema, schemaPath),
require('jsonpointer').get(schema, [""].concat(schemaPath))
))
// All evaluate to the same thing: true
Greedy mode tries to validate as much as possible
By default is-my-json-valid bails on first validation error but when greedy is
set to true it tries to validate as much as possible:
Here is a list of possible message values for errors:
is required
is the wrong type
has additional items
must be FORMAT format (FORMAT is the format property from the schema)
must be unique
must be an enum value
dependencies not set
has additional properties
referenced schema does not match
negative schema matches
pattern mismatch
no schemas match
no (or more than one) schemas match
has a remainder
has more properties than allowed
has less properties than allowed
has more items than allowed
has less items than allowed
has longer length than allowed
has less length than allowed
is less than minimum
is more than maximum
Performance
is-my-json-valid uses code generation to turn your JSON schema into basic javascript code that is easily optimizeable by v8.
At the time of writing, is-my-json-valid is the fastest validator when running
json-schema-benchmark
cosmicreals.com benchmark
jsck benchmark
themis benchmark
z-schema benchmark
If you know any other relevant benchmarks open a PR and I'll add them.
TypeScript support
This library ships with TypeScript typings. They are still early on and not perfect at the moment, but should hopefully handle the most common cases. If you find anything that doesn't work, please open an issue and we'll try to solve it.
The typings are using unknown and thus require TypeScript 3.0 or later.
Here is a quick sample of usage together with express:
import createError = require('http-errors')
import createValidator = require('is-my-json-valid')
import { Request, Response, NextFunction } from 'express'
const personValidator = createValidator({
type: 'object',
properties: {
name: { type: 'string' },
age: { type: 'number' },
},
required: [
'name'
]
})
export function post (req: Request, res: Response, next: NextFunction) {
// Here req.body is typed as: any
if (!personValidator(req.body)) {
throw createError(400, { errors: personValidator.errors })
}
// Here req.body is typed as: { name: string, age: number | undefined }
}
As you can see, the typings for is-my-json-valid will contruct an interface from the schema passed in. This allows you to work with your incoming json body in a type safe way.
Trang TopGit này là một snapshot — tab "Readme" hiển thị nguyên văn README của repo (đã bỏ link, giữ ảnh). Repo GitHub ở github.com/mafintosh/is-my-json-valid là nguồn chính thức.
mafintosh/is-my-json-valid có phải mã nguồn mở không?
Có — mafintosh/is-my-json-valid phát hành theo license MIT, nghĩa là mã nguồn mở để đọc, fork và (tùy license) tái sử dụng. Mã: github.com/mafintosh/is-my-json-valid.
mafintosh/is-my-json-valid có tag gì không?
Bản đồng bộ chưa ghi nhận topic GitHub nào cho mafintosh/is-my-json-valid. GitHub topics hiển thị ở thanh bên phải trang repo — đó là nơi đáng kiểm tra nhất.
mafintosh/is-my-json-valid có website riêng không?
TopGit chưa ghi nhận URL trang chủ cho mafintosh/is-my-json-valid. Phần README ở tab phía trên thường có link demo, hoặc xem mô tả GitHub của repo.
mafintosh/is-my-json-valid còn đang phát triển không?
Commit gần nhất trên mafintosh/is-my-json-valid là 3 tháng trước (theo timestamp GitHub). Repo có 114 fork — một chỉ báo về mức độ quan tâm của cộng đồng.
mafintosh/is-my-json-valid dùng license gì?
mafintosh/is-my-json-valid phát hành theo license MIT. Nên mở file LICENSE trên GitHub để xác nhận — license metadata đôi khi lệch với thực tế dự án.
Đọc đầy đủ README ở tab phía trên.
Vẫn đang phân vân về is-my-json-valid?
Một cú bấm sẽ gửi câu hỏi kèm trang này cho AI — xem AI nói gì về is-my-json-valid.