Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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'),
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({
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)
}
},
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
},
}).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)
}
)
})
}
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,
};
}
};
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,
};
}
};