Generates yup instances from JSON schemas.
This project is meant to be a successor to the original yup-ast which - due to licensing issues - can no longer be actively maintained. Due to time contraints, only the core functionality has been ported so far. Feel free to add any potential improvements or missing APIs from the original via PRs!
npm install --save @demvsystems/yup-ast
var transformAll = require('@demvsystems/yup-ast').transformAll;
import { transformAll } from '@demvsystems/yup-ast';
The JSON representation of a yup schema is an array with each call being an array again. The method calls start with "yup.". Each parameter is an additional array entry afterwards.
Example: To create a schema like the following:
const schema = yup.array()
.required()
.min(2)
.of(
yup.object()
.required()
.shape({
foo: yup.string().required(),
})
);
you call transformAll
like this:
const schema = transformAll([
['yup.array'],
['yup.required'],
['yup.min', 2],
['yup.of', [
['yup.object'],
['yup.required'],
['yup.shape', {
foo: [['yup.string'], ['yup.required']],
}],
]],
]);
Both can be validated the same way:
schema.isValidSync([
{ foo: 'bar' },
{ foo: 'baz' },
]); // => true
For more example use cases, have a look at the included test cases.
Please look at the releases for more information on what has changed recently.
The ISC License (ISC). Please see License File for more information.