Skip to content

Commit

Permalink
feat(context): prevent user to not used the yup option context
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul-Vandell committed Jul 22, 2020
1 parent e33e7f2 commit b08757d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 20 deletions.
81 changes: 63 additions & 18 deletions src/yup.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-console */
import * as yup from 'yup';
import { yupResolver } from './yup';

Expand Down Expand Up @@ -88,6 +89,27 @@ describe('yupResolver', () => {
});
});

it('should pass down the yup context', async () => {
const data = { name: 'eric' };
const context = { min: true };
const schemaWithContext = yup.object().shape({
name: yup
.string()
.required()
.when('$min', (min: boolean, schema: yup.StringSchema) => {
return min ? schema.min(6) : schema;
}),
});
schemaWithContext.validate = jest.fn().mockResolvedValue({});
await yupResolver(schemaWithContext)(data, context);
expect(schemaWithContext.validate).toHaveBeenCalled();
expect(schemaWithContext.validate).toHaveBeenCalledWith(data, {
abortEarly: false,
context,
});
(schemaWithContext.validate as jest.Mock).mockClear();
});

describe('errors', () => {
it('should get errors with validate all criteria fields', async () => {
const data = {
Expand Down Expand Up @@ -164,25 +186,22 @@ describe('yupResolver', () => {
}
`);
});
});

it('should pass down the yup context', async () => {
const data = { name: 'eric' };
const context = { min: true };
const schemaWithContext = yup.object().shape({
name: yup
.string()
.required()
.when('$min', (min: boolean, schema: yup.StringSchema) => {
return min ? schema.min(6) : schema;
}),
});
schemaWithContext.validate = jest.fn().mockResolvedValue({});
await yupResolver(schemaWithContext)(data, context);
expect(schemaWithContext.validate).toHaveBeenCalled();
expect(schemaWithContext.validate).toHaveBeenCalledWith(data, {
abortEarly: false,
context,
it('should return an empty error result if inner yup validation error has no path', async () => {
const data = { name: '' };
const schemaWithContext = yup.object().shape({
name: yup.string().required(),
});
schemaWithContext.validate = jest.fn().mockRejectedValue({
inner: [{ path: '', message: 'error1', type: 'required' }],
} as yup.ValidationError);
const result = await yupResolver(schemaWithContext)(data);
expect(result).toMatchInlineSnapshot(`
Object {
"errors": Object {},
"values": Object {},
}
`);
});
});
});
Expand Down Expand Up @@ -232,4 +251,30 @@ describe('validateWithSchema', () => {
}
`);
});

it('should show a log warning if yup context is used instead only on dev environment', async () => {
console.warn = jest.fn();
process.env.NODE_ENV = 'development';
await yupResolver(
{} as any,
{ context: { noContext: true } } as yup.ValidateOptions,
)({});
expect(console.warn).toHaveBeenCalledWith(
"You should not used the yup options context. Please, use the 'useForm' context object instead",
);
process.env.NODE_ENV = 'test';
(console.warn as jest.Mock).mockClear();
});

it('should show any log warning if yup context is used instead only on production environment', async () => {
console.warn = jest.fn();
process.env.NODE_ENV = 'production';
await yupResolver(
{} as any,
{ context: { noContext: true } } as yup.ValidateOptions,
)({});
expect(console.warn).not.toHaveBeenCalled();
process.env.NODE_ENV = 'test';
(console.warn as jest.Mock).mockClear();
});
});
13 changes: 11 additions & 2 deletions src/yup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const parseErrorSchema = (

export const yupResolver = <TFieldValues extends Record<string, any>>(
schema: Yup.ObjectSchema | Yup.Lazy,
options: Yup.ValidateOptions = {
options: Omit<Yup.ValidateOptions, 'context'> = {
abortEarly: false,
},
): Resolver<TFieldValues> => async (
Expand All @@ -50,10 +50,19 @@ export const yupResolver = <TFieldValues extends Record<string, any>>(
validateAllFieldCriteria = false,
) => {
try {
if (
(options as Yup.ValidateOptions).context &&
process.env.NODE_ENV === 'development'
) {
// eslint-disable-next-line no-console
console.warn(
"You should not used the yup options context. Please, use the 'useForm' context object instead",
);
}
return {
values: (await schema.validate(values, {
context,
...options,
context,
})) as any,
errors: {},
};
Expand Down

0 comments on commit b08757d

Please sign in to comment.