yup#AnySchema TypeScript Examples
The following examples show how to use
yup#AnySchema.
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 |
export function mapPropertyToYup(propertyContext: PropertyContext<any>): AnySchema<unknown> {
const property = propertyContext.property;
if (typeof property === "function") {
console.log("Error in property", propertyContext);
throw Error("PropertyBuilders can only be defined as the root properties in entity schemas, not in child properties");
}
if (property.dataType === "string") {
return getYupStringSchema(propertyContext);
} else if (property.dataType === "number") {
return getYupNumberSchema(propertyContext);
} else if (property.dataType === "boolean") {
return getYupBooleanSchema(propertyContext);
} else if (property.dataType === "map") {
return getYupMapObjectSchema(propertyContext);
} else if (property.dataType === "array") {
return getYupArraySchema(propertyContext);
} else if (property.dataType === "timestamp") {
return getYupDateSchema(propertyContext);
} else if (property.dataType === "geopoint") {
return getYupGeoPointSchema(propertyContext);
} else if (property.dataType === "reference") {
return getYupReferenceSchema(propertyContext);
}
console.error("Unsupported data type in yup mapping", property)
throw Error("Unsupported data type in yup mapping");
}
Example #2
Source File: validation.ts From firecms with MIT License | 6 votes |
function getYupGeoPointSchema({
property,
parentProperty,
customFieldValidator,
name
}: PropertyContext<GeopointProperty>): AnySchema {
let schema: ObjectSchema<any> = yup.object();
const validation = property.validation;
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?.required) {
schema = schema.required(validation.requiredMessage).nullable(true);
} else {
schema = schema.notRequired().nullable(true);
}
return schema;
}
Example #3
Source File: validation.ts From firecms with MIT License | 6 votes |
function getYupDateSchema({
property,
parentProperty,
customFieldValidator,
name
}: PropertyContext<TimestampProperty>): AnySchema | DateSchema {
if (property.autoValue) {
return yup.object().nullable(true);
}
let schema: DateSchema<any> = yup.date();
const validation = property.validation;
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) schema = schema.min(validation.min, `${property.title} must be after ${validation.min}`);
if (validation.max) schema = schema.max(validation.max, `${property.title} must be before ${validation.min}`);
} else {
schema = schema.notRequired().nullable(true);
}
return schema;
}
Example #4
Source File: validation.ts From firecms with MIT License | 6 votes |
function getYupReferenceSchema({
property,
parentProperty,
customFieldValidator,
name
}: PropertyContext<ReferenceProperty>): AnySchema {
let schema: ObjectSchema<any> = yup.object();
const validation = property.validation;
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
}));
} else {
schema = schema.notRequired().nullable(true);
}
return schema;
}