yup#NumberSchema TypeScript Examples
The following examples show how to use
yup#NumberSchema.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: validation.ts From firecms with MIT License | 6 votes |
function getYupNumberSchema({
property,
parentProperty,
customFieldValidator,
name
}: PropertyContext<NumberProperty>): NumberSchema {
const validation = property.validation;
let schema: NumberSchema<any> = yup.number().typeError("Must be a number");
if (validation) {
schema = validation.required
? schema.required(validation.requiredMessage ? validation.requiredMessage : "Required").nullable(true)
: schema.notRequired().nullable(true);
if (validation.unique && customFieldValidator && name)
schema = schema.test("unique",
"This value already exists and should be unique",
(value) => customFieldValidator({
name,
property,
parentProperty,
value
}));
if (validation.min || validation.min === 0) schema = schema.min(validation.min, `${property.title} must be higher or equal to ${validation.min}`);
if (validation.max || validation.max === 0) schema = schema.max(validation.max, `${property.title} must be lower or equal to ${validation.max}`);
if (validation.lessThan || validation.lessThan === 0) schema = schema.lessThan(validation.lessThan, `${property.title} must be higher than ${validation.lessThan}`);
if (validation.moreThan || validation.moreThan === 0) schema = schema.moreThan(validation.moreThan, `${property.title} must be lower than ${validation.moreThan}`);
if (validation.positive) schema = schema.positive(`${property.title} must be positive`);
if (validation.negative) schema = schema.negative(`${property.title} must be negative`);
if (validation.integer) schema = schema.integer(`${property.title} must be an integer`);
} else {
schema = schema.notRequired().nullable(true);
}
return schema;
}