Skip to content

Commit

Permalink
Update Superstruct version (#83)
Browse files Browse the repository at this point in the history
* chore: update deps

* chore: improve TS testing typings

* build: update module resolution

* test: update TS error

* feat: update superstruct version
  • Loading branch information
jorisre authored Nov 30, 2020
1 parent 72a775c commit 02814b0
Show file tree
Hide file tree
Showing 8 changed files with 1,073 additions and 884 deletions.
46 changes: 23 additions & 23 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,33 +55,33 @@
},
"homepage": "https://react-hook-form.com",
"devDependencies": {
"@rollup/plugin-commonjs": "^13.0.0",
"@rollup/plugin-node-resolve": "^8.0.1",
"@types/jest": "^26.0.0",
"@types/yup": "^0.29.3",
"@typescript-eslint/eslint-plugin": "^3.2.0",
"@typescript-eslint/parser": "^3.2.0",
"eslint": "^7.2.0",
"eslint-config-prettier": "^6.11.0",
"@rollup/plugin-commonjs": "^16.0.0",
"@rollup/plugin-node-resolve": "^10.0.0",
"@types/jest": "^26.0.15",
"@types/yup": "^0.29.9",
"@typescript-eslint/eslint-plugin": "^4.8.2",
"@typescript-eslint/parser": "^4.8.2",
"eslint": "^7.14.0",
"eslint-config-prettier": "^6.15.0",
"eslint-plugin-prettier": "^3.1.4",
"husky": "^4.2.5",
"jest": "^26.0.1",
"husky": "^4.3.0",
"jest": "^26.6.3",
"joi": "^17.3.0",
"lint-staged": "^10.2.10",
"matched": "^5.0.0",
"lint-staged": "^10.5.2",
"matched": "^5.0.1",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.5",
"react": "^16.13.1",
"react-hook-form": "^6.7.0",
"prettier": "^2.2.1",
"react": "^17.0.1",
"react-hook-form": "^6.12.1",
"rimraf": "^3.0.2",
"rollup": "^2.16.1",
"rollup-plugin-terser": "^6.1.0",
"rollup-plugin-typescript2": "^0.27.1",
"superstruct": "^0.8.3",
"ts-jest": "^26.1.0",
"typescript": "^3.7.5",
"yup": "^0.29.1",
"zod": "^1.11.5"
"rollup": "^2.34.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.29.0",
"superstruct": "^0.12.1",
"ts-jest": "^26.4.4",
"typescript": "^4.1.2",
"yup": "^0.31.0",
"zod": "^1.11.10"
},
"peerDependencies": {
"react-hook-form": ">=6.6.0"
Expand Down
6 changes: 1 addition & 5 deletions rollup/merge-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,7 @@ export default function mergeRollupConfig(config) {
...config.output,
},
plugins: [
resolve({
customResolveOptions: {
moduleDirectory: config.output.dir,
},
}),
resolve(),
terser({
output: { comments: false },
compress: {
Expand Down
10 changes: 5 additions & 5 deletions src/__snapshots__/superstruct.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,26 @@ Object {
"errors": Object {
"author": Object {
"id": Object {
"message": "",
"message": "Expected a number, but received: \\"test\\"",
"type": "number",
},
},
"id": Object {
"message": "",
"message": "Expected a number, but received: \\"2\\"",
"type": "number",
},
"tags": Array [
Object {
"message": "",
"message": "Expected a string, but received: 2",
"type": "string",
},
Object {
"message": "",
"message": "Expected a string, but received: 3",
"type": "string",
},
],
"title": Object {
"message": "",
"message": "Expected a string, but received: 2",
"type": "string",
},
},
Expand Down
22 changes: 13 additions & 9 deletions src/superstruct.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { struct } from 'superstruct';
import { object, number, string, boolean, array, optional } from 'superstruct';
import { superstructResolver } from './superstruct';

const Article = struct({
id: 'number',
title: 'string',
isPublished: 'boolean?',
tags: ['string'],
author: {
id: 'number',
},
const Article = object({
id: number(),
title: string(),
isPublished: optional(boolean()),
tags: array(string()),
author: object({
id: number(),
}),
});

describe('superstructResolver', () => {
Expand All @@ -21,6 +21,7 @@ describe('superstructResolver', () => {
id: 1,
},
};

expect(await superstructResolver(Article)(data)).toEqual({
values: data,
errors: {},
Expand All @@ -36,6 +37,9 @@ describe('superstructResolver', () => {
id: 'test',
},
};

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error - For testing purpose `id`'s type is wrong
expect(await superstructResolver(Article)(data)).toMatchSnapshot();
});
});
102 changes: 50 additions & 52 deletions src/superstruct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,69 +2,67 @@ import {
appendErrors,
transformToNestObject,
Resolver,
FieldValues,
ResolverSuccess,
ResolverError,
} from 'react-hook-form';
import Superstruct from 'superstruct';
import { StructError, validate, Struct, Infer } from 'superstruct';
import convertArrayToPathName from './utils/convertArrayToPathName';

const parseErrorSchema = (
error: Superstruct.StructError,
error: StructError,
validateAllFieldCriteria: boolean,
) =>
Array.isArray(error.failures)
? error.failures.reduce(
(previous: Record<string, any>, { path, message = '', type }) => {
const currentPath = convertArrayToPathName(path);
error
.failures()
.reduce((previous: Record<string, any>, { path, message = '', type }) => {
const currentPath = convertArrayToPathName(path);
return {
...previous,
...(path
? previous[currentPath] && validateAllFieldCriteria
? {
[currentPath]: appendErrors(
currentPath,
validateAllFieldCriteria,
previous,
type || '',
message,
),
}
: {
[currentPath]: previous[currentPath] || {
message,
type,
...(validateAllFieldCriteria
? {
types: { [type || '']: message || true },
}
: {}),
},
}
: {}),
};
}, {});

return {
...previous,
...(path
? previous[currentPath] && validateAllFieldCriteria
? {
[currentPath]: appendErrors(
currentPath,
validateAllFieldCriteria,
previous,
type || '',
message,
),
}
: {
[currentPath]: previous[currentPath] || {
message,
type,
...(validateAllFieldCriteria
? {
types: { [type || '']: message || true },
}
: {}),
},
}
: {}),
};
},
{},
)
: [];
type Options = Parameters<typeof validate>[2];

export const superstructResolver = <TFieldValues extends FieldValues>(
schema: Superstruct.Struct,
): Resolver<TFieldValues> => async (
values,
_,
validateAllFieldCriteria = false,
) => {
try {
return {
values: schema(values),
errors: {},
};
} catch (e) {
export const superstructResolver = <T extends Struct<any, any>>(
schema: T,
options?: Options,
): Resolver<Infer<T>> => (values, _, validateAllFieldCriteria = false) => {
const [errors, result] = validate(values, schema, options);

if (errors != null) {
return {
values: {},
errors: transformToNestObject(
parseErrorSchema(e, validateAllFieldCriteria),
parseErrorSchema(errors, validateAllFieldCriteria),
),
};
} as ResolverError<Infer<T>>;
}

return {
values: result,
errors: {},
} as ResolverSuccess<Infer<T>>;
};
4 changes: 2 additions & 2 deletions src/yup.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ describe('validateWithSchema', () => {
it('should return empty object when validate pass', async () => {
expect(
await yupResolver({
validate: () => new Promise((resolve) => resolve()),
validate: () => new Promise((resolve) => resolve(undefined)),
} as any)({}),
).toEqual({
errors: {},
Expand Down Expand Up @@ -281,7 +281,7 @@ describe('validateWithSchema', () => {
email: yup.string(),
})
.test('name', 'Email or name are required', function (value) {
return value.name || value.email;
return value && (value.name || value.email);
}),
)({ name: '', email: '' });

Expand Down
6 changes: 4 additions & 2 deletions tsconfig.jest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs"
}
"module": "commonjs",
"strictNullChecks": true
},
"include": ["src/**/*.test.ts"]
}
Loading

0 comments on commit 02814b0

Please sign in to comment.