
Security News
Open VSX Begins Implementing Pre-Publish Security Checks After Repeated Supply Chain Incidents
Following multiple malicious extension incidents, Open VSX outlines new safeguards designed to catch risky uploads earlier.
@times/schema-validator
Advanced tools
Simple functional and composable JavaScript validation library
Validate an object based on a schema:
const { objectValidator, ok, err } = require('@times/schema-validator');
const objectSchema = {
name: {
type: 'string',
required: true,
validator: s => s.length <= 10 ? ok() : err([`"${s}" was longer than 10`]),
},
age: {
type: 'number',
required: false
}
};
const isValid = objectValidator(objectSchema);
const alice = {
name: 'Alice',
age: 23
};
isValid(alice); // { valid: true, errors: [] }
const bob = {
age: 'thirty'
};
isValid(bob); // { valid: false, errors: [ `Missing required field "name"`, `Field "age" failed to typecheck (expected number)` ] }
const christopher = {
name: 'Christopher',
}
isValid(christopher); // { valid: false, errors: [ `At field "name": "Christopher" was longer than 10` ] }
Validate an array based on a schema:
const { arrayValidator, ok, err } = require('@times/schema-validator');
const arraySchema = {
type: 'number',
validator: n => n >= 10 ? ok() : err([`${n} was less than 10`])
};
const isValid = arrayValidator(arraySchema);
const numbers1 = [ 9, 10, 11 ];
isValid(numbers1); // { valid: false, errors: [ `At item 0: 9 was less than 10` ] }
const numbers2 = [ 'ten', 11 ];
isValid(numbers2); // { valid: false, errors: [ `Item "ten" failed to typecheck (expected number)` ] }
An object schema consists of field names that map to sets of properties. Each set of properties can optionally include:
type: the type of the field. Can be string, number, date, array, object, function...required: whether the field is required. Should be true or falsevalidator: a nested validator that should be applied to the contents of the fieldAn array schema can similarly have the following optional properties:
type: the type of the items in the arrayvalidator: a nested validator that should be applied to each item in the arrayTwo useful functions, objectValidator and arrayValidator, are provided by default. Both accept a schema and turn it into a validator.
If these functions are insufficient, however, there are several functions available for you to build and compose your own validators.
A validator is any function with the signature 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 all or some composition functions. For example:
const validatorOne = data => data <= 3 ? ok() : err([`Data was greater than three`]);
const validatorTwo = ...
// Composing with `all` returns a validator that will succeed if all of the given validators succeed
const composedValidator1 = all([
validatorOne,
validatorTwo
]);
const result1 = composedValidator1(data);
// Using `some` returns a validator that will succeed if at least one of the given validators succeeds
const composedValidator2 = some([
validatorOne,
validatorTwo,
]);
const result2 = composedValidator2(data);
You can of course write your own composition functions. A composition function must accept an array of validators and run them, somehow combining the Results into a single Result.
If you would like to use a schema beyond the supported object and array schemas, you can make use of the following exported functions:
fromObjectSchema: Converts an object schema to an array of validatorsfromObjectSchemaStrict: Converts an object schema to an array of validators, including a validator that checks the object has no extra fieldsfromArraySchema: Converts an array schema to an array of validatorsYou can also write your own schema conversion functions should you wish.
The resulting list of validators can then be combined into a single validator using all, some or your own composition function; this is how the default objectValidator and arrayValidator helpers work.
Pull requests are very welcome. Please include a clear description of any changes, and full test coverage.
During development you can run tests with
yarn test
The library uses Flow for type checking. You can run Flow with
yarn flow
Elliot Davies (elliot.davies@the-times.co.uk)
FAQs
Basic schema validator for JS objects and arrays
The npm package @times/schema-validator receives a total of 4 weekly downloads. As such, @times/schema-validator popularity was classified as not popular.
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
Following multiple malicious extension incidents, Open VSX outlines new safeguards designed to catch risky uploads earlier.

Research
/Security News
Threat actors compromised four oorzc Open VSX extensions with more than 22,000 downloads, pushing malicious versions that install a staged loader, evade Russian-locale systems, pull C2 from Solana memos, and steal macOS credentials and wallets.

Security News
Lodash 4.17.23 marks a security reset, with maintainers rebuilding governance and infrastructure to support long-term, sustainable maintenance.