
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
the-json-library
Advanced tools
A lightweight validation library, which validates JSON data against a JSON schema.
A lightweight JSON schema validation library for JavaScript that can be used in both browser and Node.js environments.
npm install the-json-library
import { validate } from 'the-json-library';
// Define a schema
const schema = {
type: 'object',
required: ['name', 'email'],
properties: {
name: { type: 'string' },
email: { type: 'string', format: 'email' },
age: { type: 'integer', minimum: 0 }
}
};
// Validate some data
const data = {
name: 'John Doe',
email: 'john@example.com',
age: 30
};
const result = validate(data);
if (result.isValid) {
console.log('Validation successful!');
} else {
console.error('Validation errors:', result.errors);
}
You can also use the static method without creating an instance:
import { validate } from 'the-json-library';
const result = validate(data, schema);
if (result.isValid) {
console.log('Validation successful!');
} else {
console.error('Validation errors:', result.errors);
}
By default, the library rejects any properties not defined in the schema:
// Schema only defines name and email
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' }
}
};
// Data contains an extra property 'age'
const data = {
name: 'John Doe',
email: 'john@example.com',
age: 30 // This will cause validation failure
};
const result = validate(data, schema);
// result.isValid will be false
// result.errors will contain an error about 'age' being an additional property
To allow additional properties, set additionalProperties
to true
:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' }
},
additionalProperties: true // Allow any additional properties
};
// Now this will pass validation
const data = {
name: 'John Doe',
email: 'john@example.com',
age: 30 // This is now allowed
};
You can also provide a schema for additional properties:
const schema = {
type: 'object',
properties: {
name: { type: 'string' },
email: { type: 'string' }
},
// All additional properties must be numbers
additionalProperties: { type: 'number' }
};
// This will pass validation
const data = {
name: 'John Doe',
email: 'john@example.com',
age: 30, // Valid additional property (number)
score: 95 // Valid additional property (number)
};
The validation result contains:
isValid
- boolean indicating if the validation passederrors
- array of error objects with:
path
- the path to the property that failed validationmessage
- description of the errorstring
number
integer
boolean
array
object
null
minLength
/ maxLength
- string length constraintspattern
- regular expression patternformat
- predefined formats (email, date, date-time, uri)minimum
/ maximum
- value constraintsproperties
- schema for each propertyrequired
- list of required propertiesadditionalProperties
- controls whether properties not defined in the schema are alloweditems
- schema for array itemsenum
- list of allowed valuesSchemas should follow a simplified JSON Schema format. Here's an example of a more complex schema:
{
"type": "object",
"required": ["id", "name", "metadata"],
"properties": {
"id": { "type": "integer" },
"name": { "type": "string", "minLength": 1 },
"email": { "type": "string", "format": "email" },
"tags": {
"type": "array",
"items": { "type": "string" }
},
"metadata": {
"type": "object",
"properties": {
"created": { "type": "string", "format": "date-time" },
"status": { "enum": ["active", "inactive", "pending"] }
},
"additionalProperties": false
}
},
"additionalProperties": false // No additional root properties allowed
}
MIT
FAQs
A lightweight validation library, which validates JSON data against a JSON schema.
The npm package the-json-library receives a total of 1 weekly downloads. As such, the-json-library popularity was classified as not popular.
We found that the-json-library demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.