Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
is-my-json-valid
Advanced tools
A JSONSchema / orderly validator that uses code generation to be extremely fast
The is-my-json-valid npm package is a JSON schema validator that is fast and simple to use. It allows you to validate JSON data against a schema, ensuring that the data conforms to the expected structure and types.
Basic JSON Validation
This feature allows you to validate a simple JSON object against a schema. In this example, the schema expects an object with a string 'name' and a number 'age'. The provided JSON object meets these criteria, so the validation returns true.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } } });
const valid = validate({ name: 'John', age: 30 });
console.log(valid); // true
Custom Error Messages
This feature provides detailed error messages when validation fails. In this example, the 'age' property is a string instead of a number, so the validation fails and the errors are logged.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { name: { type: 'string' }, age: { type: 'number' } } }, { verbose: true });
const valid = validate({ name: 'John', age: 'thirty' });
if (!valid) console.log(validate.errors);
Formats and Custom Formats
This feature allows you to use predefined formats like 'email' to validate specific types of strings. In this example, the 'email' property must be a valid email address.
const validator = require('is-my-json-valid');
const validate = validator({ type: 'object', properties: { email: { type: 'string', format: 'email' } } });
const valid = validate({ email: 'test@example.com' });
console.log(valid); // true
AJV (Another JSON Schema Validator) is a highly efficient and feature-rich JSON schema validator. It supports JSON Schema draft-07 and provides advanced features like asynchronous validation, custom keywords, and more. Compared to is-my-json-valid, AJV offers more extensive support for JSON Schema specifications and additional features.
The jsonschema package is a simple and easy-to-use JSON schema validator. It supports JSON Schema draft-04 and is suitable for basic validation needs. While it may not be as fast as is-my-json-valid, it provides a straightforward API for validating JSON data.
Joi is a powerful schema description language and data validator for JavaScript objects. It allows you to define schemas using a fluent API and provides extensive validation capabilities. Unlike is-my-json-valid, Joi is not limited to JSON Schema and offers more flexibility in defining and validating data structures.
A JSONSchema/orderly validator that uses code generation to be extremely fast
npm install is-my-json-valid
Simply pass a schema to compile it
var validator = require('is-my-json-valid')
var validate = validator({
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 = validate('{"type": ... }')
Or pass a orderly schema
var validate = validate(
'object {'+
' string name;'+
'}*;'
)
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')
// or load a orderly schema
var validate = validator('my-orderly-schema.schema')
This is module is fast
When running the cosmicrealms.com benchmark it yields the following results on my macbook air
is-my-json-valid v4 total time (3881) and per document time: 0.0019405 <-- or 2500x faster than jayschema
amanda v3 total time (27121) and per document time: 1.35605
jayschema v4 total time (99449) and per document time: 4.97245
joi v3 total time (11949) and per document time: 0.59745
json-gate v3 total time (1443) and per document time: 0.07215
json-schema v3 total time (1318) and per document time: 0.0659
JSV v3 total time (33495) and per document time: 1.67475
schema v2 total time (1309) and per document time: 0.06545
tv4 v4 total time (703) and per document time: 0.03515
z-schema v4 total time (3188) and per document time: 0.1594
As seen above is-my-json-valid
is 2500x faster than the slowest and ~20x faster than the second fastest
MIT
FAQs
A [JSONSchema](https://json-schema.org/) validator that uses code generation to be extremely fast.
The npm package is-my-json-valid receives a total of 342,777 weekly downloads. As such, is-my-json-valid popularity was classified as popular.
We found that is-my-json-valid demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.