
Security News
n8n Tops 2025 JavaScript Rising Stars as Workflow Platforms Gain Momentum
n8n led JavaScript Rising Stars 2025 by a wide margin, with workflow platforms seeing the largest growth across categories.
@times/schema-validator
Advanced tools
Basic schema validator for JS objects and arrays
Objects:
const { objectValidator } = require('@times/schema-validator');
const objectSchema = {
name: {
type: 'string',
required: true,
predicates: [
{
test: s => s.length <= 20,
onError: s => `${s} was longer than 20`
}
]
},
age: {
type: 'number',
required: false
}
};
const isValid = objectValidator(objectSchema);
const alice = {
name: 'Alice Smith',
age: 23
};
const bob = {
age: 'thirty'
};
isValid(alice); // { valid: true, errors: [] }
isValid(bob); // { valid: false, errors: [ ... ] }
Arrays:
const { arrayValidator } = require('@times/schema-validator');
const arraySchema = {
type: 'number',
predicates: [
{
test: n => n >= 10,
onError: n => `${n} was less than 10`
}
]
};
const isValid = arrayValidator(arraySchema);
const numbers1 = [ 1, 2, 3 ];
const numbers2 = [ 9, 10, 11 ];
const numbers3 = [ 'one', 'two' ];
isValid(numbers1); // { valid: true, errors: [] }
isValid(numbers2); // { valid: false, errors: [ ... ] }
isValid(numbers3); // { valid: false, errors: [ ... ] }
An object schema consists of field names that map to sets of properties. Each set of properties can include:
type (required): the type of the field. Can be string, number, date, array, objectrequired (optional): whether the field is required. Can be true or false, or omittedpredicates (optional): an array of predicates that should be applied to the contents of the fieldschemaValidator (optional): if the field is an array or object, you can provide a validator that should be applied to the contents of the fieldAn array schema can have the following properties:
type (required): the type of the items in the arraypredicates (optional): an array of predicates that should be applied to every item in the arrayschemaValidator (optional): if the array items are arrays or objects, you can provide a validator that should be applied to each array itemPredicates are boolean functions that can be applied to array items. They receive the array item as an argument.
You must also specify an error message, which will be used when the predicate returns false. The error message is specified through an onError function that also receives the array item as an argument.
For example:
{
test: s => s.includes('xyz'),
onError: s => `The string "${s}" did not include "xyz"`
}
Two validators, objectValidator and arrayValidator, are provided by default. If these are insufficient, however, you can build and compose your own validators.
A validator is a function with the signature schema -> data -> Result, where a Result can be constructed using the provided ok() or err() functions. err() accepts an array of error messages.
To chain multiple validators together you can use the compose function. For example:
const validatorOne = schema => data =>
data.hasOwnProperty('one') ? ok() : err([`No property "one"`]);
const validatorTwo = ...
const composedValidator = compose(
validatorOne,
validatorTwo
);
const result = composedValidator(schema)(data);
Composed validators are run in order until either one of them returns an error or they all succeed.
Pull requests are very welcome. Please include a clear description of any changes, and full test coverage.
During development you can run tests with
npm test
Elliot Davies (elliot.davies@the-times.co.uk)
FAQs
Basic schema validator for JS objects and arrays
We found that @times/schema-validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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
n8n led JavaScript Rising Stars 2025 by a wide margin, with workflow platforms seeing the largest growth across categories.

Security News
The U.S. government is rolling back software supply chain mandates, shifting from mandatory SBOMs and attestations to a risk-based approach.

Security News
crates.io adds a Security tab backed by RustSec advisories and narrows trusted publishing paths to reduce common CI publishing risks.