yup#ArraySchema TypeScript Examples
The following examples show how to use
yup#ArraySchema.
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 | 4 votes |
function getYupArraySchema({
property,
parentProperty,
customFieldValidator,
name
}: PropertyContext<ArrayProperty>): ArraySchema<any> {
let schema: ArraySchema<any> = yup.array();
if (property.of) {
schema = schema.of(mapPropertyToYup({
property: property.of,
parentProperty: property
}));
const arrayUniqueFields = hasUniqueInArrayModifier(property.of);
if (arrayUniqueFields) {
if (typeof arrayUniqueFields === "boolean") {
schema = schema.uniqueInArray(v => v, `${property.title} should have unique values within the array`);
} else if (Array.isArray(arrayUniqueFields)) {
arrayUniqueFields.forEach(([name, childProperty]) => {
schema = schema.uniqueInArray(v => v && v[name], `${property.title} → ${childProperty.title ?? name}: should have unique values within the array`);
}
);
}
}
}
const validation = property.validation;
if (validation) {
schema = validation.required
? schema.required(validation?.requiredMessage ? validation.requiredMessage : "Required").nullable(true)
: schema.notRequired().nullable(true);
if (validation.min || validation.min === 0) schema = schema.min(validation.min, `${property.title} should be min ${validation.min} entries long`);
if (validation.max) schema = schema.max(validation.max, `${property.title} should be max ${validation.max} entries long`);
} else {
schema = schema.notRequired().nullable(true);
}
return schema;
}