3h-validate
v0.8.2
Published
A runtime validator.
Downloads
42
Maintainers
Readme
3h-validate
A runtime validator.
Links
Examples
Type Validation
import { types } from '3h-validate';
// or: const { types } = HV;
const validator = types.number({
integersOnly: true,
min: 0,
includeMin: false,
});
if (validator.test(1)) {
console.log('1 is a valid value.');
}
validator.validate(0); // RangeError: the number is too small
Versioned Data Management
import { types, Specification } from '3h-validate';
// or: const { types, Specification } = HV;
const spec1 = new Specification({
version: 'v1',
validator: types.number(),
});
const spec2 = new Specification({
version: 'v2',
validator: types.string(),
previousSpecification: spec1,
updater: String,
});
const versionedData = {
version: 'v1',
data: 3.14,
};
console.log(spec1.test(versionedData)); // true
console.log(spec2.transform(versionedData));
// { version: 'v2', data: '3.14' }