Socket
Socket
Sign inDemoInstall

jsonschema

Package Overview
Dependencies
Maintainers
2
Versions
38
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsonschema

A fast and easy to use JSON Schema validator


Version published
Maintainers
2
Created

What is jsonschema?

The jsonschema npm package is a JSON Schema validator that can validate JavaScript objects using JSON Schemas. It is commonly used to ensure that data conforms to a predefined schema, and it can be used to validate data at runtime, as part of a testing suite, or during development to ensure data integrity.

What are jsonschema's main functionalities?

Validation

This feature allows you to validate a JavaScript object against a JSON Schema. The code sample creates a new Validator instance, defines a schema with required properties, and validates a sample data object against this schema. It outputs an empty array if the data is valid or a list of errors if it is invalid.

{"const Validator = require('jsonschema').Validator; const v = new Validator(); const schema = { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer', 'minimum': 0 } }, 'required': ['name', 'age'] }; const sampleData = { 'name': 'John Doe', 'age': 25 }; const validation = v.validate(sampleData, schema); console.log(validation.errors); // [] if valid, or list of errors if invalid."}

Custom Formats

This feature allows you to define custom formats for validation. The code sample shows how to add a custom format to the Validator instance and then use it in a schema to validate data.

{"const Validator = require('jsonschema').Validator; const v = new Validator(); v.customFormats.myFormat = function(input) { return input === 'specialValue'; }; const schema = { 'type': 'string', 'format': 'myFormat' }; const sampleData = 'specialValue'; const validation = v.validate(sampleData, schema); console.log(validation.valid); // true if valid, or false if invalid."}

Asynchronous Validation

This feature allows for asynchronous validation, which can be useful when dealing with complex validations that may require I/O operations. The code sample demonstrates how to perform validation asynchronously using a callback function.

{"const Validator = require('jsonschema').Validator; const v = new Validator(); const schema = { 'type': 'object', 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'integer', 'minimum': 0 } }, 'required': ['name', 'age'] }; const sampleData = { 'name': 'John Doe', 'age': 25 }; v.validate(sampleData, schema, function(errors, valid) { console.log(valid); // true if valid, or false if invalid });"}

Other packages similar to jsonschema

Keywords

FAQs

Package last updated on 17 May 2022

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc