Skip to content

Commit

Permalink
feat: add error's ref (#127)
Browse files Browse the repository at this point in the history
* feat: add error's ref

* test: add error's ref + extract fixtures

* test: add nested test

* refactor: rename toNestObject to toNestError
  • Loading branch information
jorisre authored Feb 3, 2021
1 parent 19dfa9b commit fd9c5bc
Show file tree
Hide file tree
Showing 24 changed files with 794 additions and 373 deletions.
74 changes: 74 additions & 0 deletions joi/src/__tests__/__fixtures__/data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import * as Joi from 'joi';

import { Field, InternalFieldName } from 'react-hook-form';

export const schema = Joi.object({
username: Joi.string().alphanum().min(3).max(30).required(),
password: Joi.string().pattern(new RegExp('^[a-zA-Z0-9]{3,30}$')).required(),
repeatPassword: Joi.ref('password'),
accessToken: [Joi.string(), Joi.number()],
birthYear: Joi.number().integer().min(1900).max(2013),
email: Joi.string().email({
minDomainSegments: 2,
tlds: { allow: ['com', 'net'] },
}),
tags: Joi.array().items(Joi.string()).required(),
enabled: Joi.boolean().required(),
like: Joi.array()
.items(Joi.object({ id: Joi.number(), name: Joi.string().length(4) }))
.optional(),
});

interface Data {
username: string;
password: string;
repeatPassword: string;
accessToken?: number | string;
birthYear?: number;
email?: string;
tags: string[];
enabled: boolean;
like: { id: number; name: string }[];
}

export const validData: Data = {
username: 'Doe',
password: 'Password123',
repeatPassword: 'Password123',
birthYear: 2000,
email: '[email protected]',
tags: ['tag1', 'tag2'],
enabled: true,
like: [
{
id: 1,
name: 'name',
},
],
};

export const invalidData = {
password: '___',
email: '',
birthYear: 'birthYear',
like: [{ id: 'z' }],
};

export const fields: Record<InternalFieldName, Field['_f']> = {
username: {
ref: { name: 'username' },
name: 'username',
},
password: {
ref: { name: 'password' },
name: 'password',
},
email: {
ref: { name: 'email' },
name: 'email',
},
birthday: {
ref: { name: 'birthday' },
name: 'birthday',
},
};
90 changes: 90 additions & 0 deletions joi/src/__tests__/__snapshots__/joi.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,47 @@ Object {
"errors": Object {
"birthYear": Object {
"message": "\\"birthYear\\" must be a number",
"ref": undefined,
"type": "number.base",
},
"email": Object {
"message": "\\"email\\" is not allowed to be empty",
"ref": Object {
"name": "email",
},
"type": "string.empty",
},
"enabled": Object {
"message": "\\"enabled\\" is required",
"ref": undefined,
"type": "any.required",
},
"like": Array [
Object {
"id": Object {
"message": "\\"like[0].id\\" must be a number",
"ref": undefined,
"type": "number.base",
},
},
],
"password": Object {
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
"ref": Object {
"name": "password",
},
"type": "string.pattern.base",
},
"tags": Object {
"message": "\\"tags\\" is required",
"ref": undefined,
"type": "any.required",
},
"username": Object {
"message": "\\"username\\" is required",
"ref": Object {
"name": "username",
},
"type": "any.required",
},
},
Expand All @@ -37,26 +58,47 @@ Object {
"errors": Object {
"birthYear": Object {
"message": "\\"birthYear\\" must be a number",
"ref": undefined,
"type": "number.base",
},
"email": Object {
"message": "\\"email\\" is not allowed to be empty",
"ref": Object {
"name": "email",
},
"type": "string.empty",
},
"enabled": Object {
"message": "\\"enabled\\" is required",
"ref": undefined,
"type": "any.required",
},
"like": Array [
Object {
"id": Object {
"message": "\\"like[0].id\\" must be a number",
"ref": undefined,
"type": "number.base",
},
},
],
"password": Object {
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
"ref": Object {
"name": "password",
},
"type": "string.pattern.base",
},
"tags": Object {
"message": "\\"tags\\" is required",
"ref": undefined,
"type": "any.required",
},
"username": Object {
"message": "\\"username\\" is required",
"ref": Object {
"name": "username",
},
"type": "any.required",
},
},
Expand All @@ -69,41 +111,65 @@ Object {
"errors": Object {
"birthYear": Object {
"message": "\\"birthYear\\" must be a number",
"ref": undefined,
"type": "number.base",
"types": Object {
"number.base": "\\"birthYear\\" must be a number",
},
},
"email": Object {
"message": "\\"email\\" is not allowed to be empty",
"ref": Object {
"name": "email",
},
"type": "string.empty",
"types": Object {
"string.empty": "\\"email\\" is not allowed to be empty",
},
},
"enabled": Object {
"message": "\\"enabled\\" is required",
"ref": undefined,
"type": "any.required",
"types": Object {
"any.required": "\\"enabled\\" is required",
},
},
"like": Array [
Object {
"id": Object {
"message": "\\"like[0].id\\" must be a number",
"ref": undefined,
"type": "number.base",
"types": Object {
"number.base": "\\"like[0].id\\" must be a number",
},
},
},
],
"password": Object {
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
"ref": Object {
"name": "password",
},
"type": "string.pattern.base",
"types": Object {
"string.pattern.base": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
},
},
"tags": Object {
"message": "\\"tags\\" is required",
"ref": undefined,
"type": "any.required",
"types": Object {
"any.required": "\\"tags\\" is required",
},
},
"username": Object {
"message": "\\"username\\" is required",
"ref": Object {
"name": "username",
},
"type": "any.required",
"types": Object {
"any.required": "\\"username\\" is required",
Expand All @@ -119,41 +185,65 @@ Object {
"errors": Object {
"birthYear": Object {
"message": "\\"birthYear\\" must be a number",
"ref": undefined,
"type": "number.base",
"types": Object {
"number.base": "\\"birthYear\\" must be a number",
},
},
"email": Object {
"message": "\\"email\\" is not allowed to be empty",
"ref": Object {
"name": "email",
},
"type": "string.empty",
"types": Object {
"string.empty": "\\"email\\" is not allowed to be empty",
},
},
"enabled": Object {
"message": "\\"enabled\\" is required",
"ref": undefined,
"type": "any.required",
"types": Object {
"any.required": "\\"enabled\\" is required",
},
},
"like": Array [
Object {
"id": Object {
"message": "\\"like[0].id\\" must be a number",
"ref": undefined,
"type": "number.base",
"types": Object {
"number.base": "\\"like[0].id\\" must be a number",
},
},
},
],
"password": Object {
"message": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
"ref": Object {
"name": "password",
},
"type": "string.pattern.base",
"types": Object {
"string.pattern.base": "\\"password\\" with value \\"___\\" fails to match the required pattern: /^[a-zA-Z0-9]{3,30}$/",
},
},
"tags": Object {
"message": "\\"tags\\" is required",
"ref": undefined,
"type": "any.required",
"types": Object {
"any.required": "\\"tags\\" is required",
},
},
"username": Object {
"message": "\\"username\\" is required",
"ref": Object {
"name": "username",
},
"type": "any.required",
"types": Object {
"any.required": "\\"username\\" is required",
Expand Down
Loading

0 comments on commit fd9c5bc

Please sign in to comment.