yup#ObjectSchema TypeScript Examples

The following examples show how to use yup#ObjectSchema. 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: schemas.ts    From abacus with GNU General Public License v2.0 6 votes vote down vote up
/**
 * The yup equivalant of _.pick, produces a subset of the original schema.
 *
 * @param schema A yup object schema
 * @param props Properties to pick
 * @param value See yup.reach
 * @param context See yup.reach
 */
export function yupPick(
  schema: yup.ObjectSchema,
  props: string[],
  value?: unknown,
  context?: unknown,
  // eslint-disable-next-line @typescript-eslint/ban-types
): ObjectSchema<{} | undefined> {
  return yup.object(_.fromPairs(props.map((prop) => [prop, yup.reach(schema, prop, value, context)])))
}
Example #2
Source File: validation.ts    From firecms with MIT License 6 votes vote down vote up
export function getYupEntitySchema<M>(properties: Properties<M>,
                                      customFieldValidator?: CustomFieldValidator): ObjectSchema<any> {
    const objectSchema: any = {};
    Object.entries(properties).forEach(([name, property]) => {
        objectSchema[name] = mapPropertyToYup({
            property,
            customFieldValidator,
            name
        });
    });
    return yup.object().shape(objectSchema);
}
Example #3
Source File: validation.ts    From firecms with MIT License 6 votes vote down vote up
export function getYupMapObjectSchema({
                                                                            property,
                                                                            parentProperty,
                                                                            customFieldValidator,
                                                                            name
                                                                        }: PropertyContext<MapProperty>): ObjectSchema<any> {
    const objectSchema: any = {};
    if (property.properties)
        Object.entries(property.properties).forEach(([childName, childProperty]: [string, Property]) => {
            objectSchema[childName] = mapPropertyToYup({
                property: childProperty,
                parentProperty: property,
                customFieldValidator,
                name: `${name}[${childName}]`
            });
        });
    return yup.object().shape(objectSchema);
}
Example #4
Source File: validation.ts    From firecms with MIT License 6 votes vote down vote up
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 #5
Source File: validation.ts    From firecms with MIT License 6 votes vote down vote up
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;
}