How to use the yup.reach function in yup

To help you get started, we’ve selected a few yup examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github DefinitelyTyped / DefinitelyTyped / types / yup / yup-tests.ts View on Github external
SchemaDescription,
    TestOptions,
    ValidateOptions,
    NumberSchema,
    TestContext,
    LocaleObject,
} from 'yup';

// reach function
const schema1 = yup.object().shape({
    nested: yup.object().shape({
        arr: yup.array().of(yup.object().shape({ num: yup.number().max(4) })),
    }),
});
reach(schema1, 'nested.arr.num');
reach(schema1, 'nested.arr[].num');

// isSchema function
const isSchemaResult1: boolean = isSchema(schema1);
const isSchemaResult2: boolean = isSchema({});

// addMethod function
yup.addMethod(yup.number, 'minimum', function(this, minValue: number, message: string) {
    return this.min(minValue, message);
});
yup.addMethod(yup.date, 'newMethod', function(this: yup.DateSchema, date: Date, message?: string) {
    return this.max(date, message);
});

// ref function
const schema2 = yup.object().shape({
    baz: yup.ref('foo.bar'),
github DefinitelyTyped / DefinitelyTyped / types / yup / yup-tests.ts View on Github external
MixedSchema,
    SchemaDescription,
    TestOptions,
    ValidateOptions,
    NumberSchema,
    TestContext,
    LocaleObject,
} from 'yup';

// reach function
const schema1 = yup.object().shape({
    nested: yup.object().shape({
        arr: yup.array().of(yup.object().shape({ num: yup.number().max(4) })),
    }),
});
reach(schema1, 'nested.arr.num');
reach(schema1, 'nested.arr[].num');

// isSchema function
const isSchemaResult1: boolean = isSchema(schema1);
const isSchemaResult2: boolean = isSchema({});

// addMethod function
yup.addMethod(yup.number, 'minimum', function(this, minValue: number, message: string) {
    return this.min(minValue, message);
});
yup.addMethod(yup.date, 'newMethod', function(this: yup.DateSchema, date: Date, message?: string) {
    return this.max(date, message);
});

// ref function
const schema2 = yup.object().shape({
github streamr-dev / streamr-platform / app / src / auth / components / ResetPasswordPage / index.jsx View on Github external
useOnMount(() => {
        const token = qs.parse(search).t || ''

        // Set and validate token on mount.
        setFormField('token', token)

        yup
            .object()
            .shape({
                token: yup.reach(schemas[0], 'token'),
            })
            .validate({
                token,
            })
            .then(
                () => {
                    if (mountedRef.current) {
                        // To make sure that the resetPassword token doesn't stick in the browser history
                        replace(pathname)
                    }
                },
                ({ message }: yup.ValidationError) => {
                    if (mountedRef.current) {
                        setFieldError('password', message)
                    }
                },
github jquense / react-input-message / example / dev.jsx View on Github external
validate( path, input) {
    // The state is updated by widgets, but isn't always synchronous so we check _pendingState first
    var state = this._pendingState || this.state 

    // `reach()` pulls out a child schema so we can test a single path
    var field = yup.reach(schema, path)

    // we also need the specific value for this path
    var value = getter(path)(state);

    return field.validate(value, { strict: true })
      .then(() => void 0)       // if valid return nothing
      .catch(err => err.errors) // if invalid return the errors for this field
  },
github streamr-dev / streamr-platform / app / src / auth / pages / RegisterPage / index.jsx View on Github external
}).invite || '', () => {
            yup
                .object()
                .shape({
                    invite: yup.reach(schemas[0], 'invite'),
                })
                .validate(this.props.form)
                .then(
                    () => {
                        // To make sure that the registerPage invite doesn't stick in the browser history
                        props.history.replace(props.location.pathname)
                    },
                    (error: yup.ValidationError) => {
                        setFieldError('name', error.message)
                    }
                )
        })
    }
github auxilincom / auxilin / web / src / client / helpers / validation / index.js View on Github external
export const validateField = async function(obj: T, field: string, schema: ObjectSchema): Promise {
  const newSchema: ObjectSchema = reach(schema, field);

  try {
    const value: T = await newSchema.validate(_get(obj, field), yupOptions);
    return {
      errors: {},
      value,
    };
  } catch (error) {
    const errors: ValidationErrorsType = parseErrors(error, field);
    return {
      value: obj,
      errors,
    };
  }
};
github paralect / koa-react-starter / src / client / helpers / validation / index.js View on Github external
export const validateField = async (obj, field, schema) => {
  const newSchema = yup.reach(schema, field);

  try {
    const value = await newSchema.validate(_.get(obj, field), yupOptions);
    return {
      errors: {},
      value,
    };
  } catch (error) {
    const errors = parseErrors(error, field);
    return {
      value: obj,
      errors,
    };
  }
};