Skip to content

Commit

Permalink
Merge branch 'master' into docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kestarumper committed Nov 29, 2024
2 parents f025d6f + d606503 commit 3d07edb
Show file tree
Hide file tree
Showing 15 changed files with 210 additions and 107 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## [v4.0.0-beta.4](https://github.com/vazco/uniforms/tree/v4.0.0-beta.3) (2024-11-18)

- **Added:** Support for nested ZodEffects [\#1382](https://github.com/vazco/uniforms/pull/1382)

## [v4.0.0-beta.3](https://github.com/vazco/uniforms/tree/v4.0.0-beta.3) (2024-10-31)

- **Added:** Update MUI to v6 [\#1381](https://github.com/vazco/uniforms/pull/1381)
- **Fixed:** Updated supported version of React in AntD theme [\#1380](https://github.com/vazco/uniforms/pull/1380)

## [v4.0.0-beta.2](https://github.com/vazco/uniforms/tree/v4.0.0-beta.2) (2024-10-29)

- **Fixed:** Fixed `tslib` issue [\#1378](https://github.com/vazco/uniforms/pull/1378)
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-antd/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-antd",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -33,8 +33,8 @@
],
"peerDependencies": {
"antd": "^5.0.0",
"react": "^18.0.0 || ^17.0.0 || ^16.8.0",
"uniforms": "4.0.0-beta.2"
"react": "^18.0.0 || ^17.0.0 || ^16.9.0",
"uniforms": "4.0.0-beta.4"
},
"dependencies": {
"classnames": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bootstrap4/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-bootstrap4",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -29,7 +29,7 @@
],
"peerDependencies": {
"react": "^18.0.0 || ^17.0.0 || ^16.8.0",
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"dependencies": {
"classnames": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bootstrap5/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-bootstrap5",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -29,7 +29,7 @@
],
"peerDependencies": {
"react": "^18.0.0 || ^17.0.0 || ^16.8.0",
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"dependencies": {
"classnames": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bridge-json-schema/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-bridge-json-schema",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -33,7 +33,7 @@
"src/*.tsx"
],
"peerDependencies": {
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"dependencies": {
"invariant": "^2.0.0",
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bridge-simple-schema-2/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-bridge-simple-schema-2",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"peerDependencies": {
"simpl-schema": "^1.0.0 || ^0.0.4",
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"devDependencies": {
"@types/invariant": "2.2.37",
Expand Down
104 changes: 92 additions & 12 deletions packages/uniforms-bridge-zod/__tests__/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
enum as enum_,
function as function_,
instanceof as instanceof_,
infer as infer_,
intersection,
lazy,
literal,
Expand Down Expand Up @@ -43,7 +44,8 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(issues?.[0]);
Expand All @@ -52,7 +54,8 @@ describe('ZodBridge', () => {

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(null);
Expand All @@ -65,7 +68,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const issues = error?.issues;
expect(bridge.getError('a', error)).toBe(null);
Expand All @@ -84,11 +88,36 @@ describe('ZodBridge', () => {
path: ['b'],
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});

it('works with chained refined schema', () => {
const firstErrorMessage = 'Different values';
const secondErrorMessage = 'Different results';

const schema = object({
a: number(),
b: number(),
})
.refine(({ a, b }) => a === b, {
message: firstErrorMessage,
path: ['b'],
})
.refine(({ a, b }) => a % 2 === b % 3, {
message: secondErrorMessage,
path: ['b'],
});

type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 1, b: 2 });
expect(error?.issues?.[0]?.message).toBe(firstErrorMessage);
expect(error?.issues?.[1]?.message).toBe(secondErrorMessage);
});

it('works with super refined schema', () => {
const errorMessage = 'Different values';

Expand All @@ -104,10 +133,43 @@ describe('ZodBridge', () => {
}
});

const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 'a', b: 'b' });
expect(error?.issues?.[0]?.message).toBe(errorMessage);
});

it('works with chained super refined schema', () => {
const firstErrorMessage = 'Different values';
const secondErrorMessage = 'Different results';

const schema = object({
a: number(),
b: number(),
})
.superRefine((val, ctx) => {
if (val.a !== val.b) {
ctx.addIssue({
code: ZodIssueCode.custom,
message: firstErrorMessage,
});
}
})
.superRefine((val, ctx) => {
if (val.a % 2 !== val.b % 3) {
ctx.addIssue({
code: ZodIssueCode.custom,
message: secondErrorMessage,
});
}
});

type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: 1, b: 2 });
expect(error?.issues?.[0]?.message).toBe(firstErrorMessage);
expect(error?.issues?.[1]?.message).toBe(secondErrorMessage);
});
});

describe('#getErrorMessage', () => {
Expand All @@ -120,7 +182,8 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe(issues?.[0].message);
Expand All @@ -129,7 +192,8 @@ describe('ZodBridge', () => {

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe('');
Expand All @@ -142,7 +206,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const issues = error?.issues;
expect(bridge.getErrorMessage('a', error)).toBe('');
Expand All @@ -167,15 +232,17 @@ describe('ZodBridge', () => {

it('works with simple types', () => {
const schema = object({ a: string(), b: number() });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({});
const messages = ['A: Required', 'B: Required'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
});

it('works with arrays', () => {
const schema = object({ a: array(array(string())) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: [['x', 'y', 0], [1]] });
const messages = [
'A (0, 2): Expected string, received number',
Expand All @@ -186,7 +253,8 @@ describe('ZodBridge', () => {

it('works with nested objects', () => {
const schema = object({ a: object({ b: object({ c: string() }) }) });
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const error = bridge.getValidator()({ a: { b: { c: 1 } } });
const messages = ['C: Expected string, received number'];
expect(bridge.getErrorMessages(error)).toEqual(messages);
Expand Down Expand Up @@ -244,6 +312,17 @@ describe('ZodBridge', () => {
const bridge = new ZodBridge({ schema });
expect(bridge.getField('')).toBe(schema._def.schema);
});

it('works with nested ZodEffects', () => {
const schema = object({})
.refine(data => data)
.refine(data => data)
.refine(data => data);
const bridge = new ZodBridge({ schema });
expect(bridge.getField('')).toBe(
schema._def.schema._def.schema._def.schema,
);
});
});

describe('#getInitialValue', () => {
Expand Down Expand Up @@ -686,7 +765,8 @@ describe('ZodBridge', () => {
describe('#getValidator', () => {
it('return a function', () => {
const schema = object({});
const bridge = new ZodBridge({ schema });
type SchemaType = infer_<typeof schema>;
const bridge = new ZodBridge<SchemaType>({ schema });
const validator = bridge.getValidator();
expect(validator).toEqual(expect.any(Function));
expect(validator({})).toEqual(null);
Expand Down
4 changes: 2 additions & 2 deletions packages/uniforms-bridge-zod/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-bridge-zod",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -39,7 +39,7 @@
},
"peerDependencies": {
"zod": "^3.0.0",
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"devDependencies": {
"@types/invariant": "2.2.37",
Expand Down
11 changes: 5 additions & 6 deletions packages/uniforms-bridge-zod/src/ZodBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import {
ZodNumberDef,
ZodObject,
ZodOptional,
ZodRawShape,
ZodString,
ZodType,
} from 'zod';
Expand Down Expand Up @@ -55,15 +54,15 @@ type Option<Value> = {
value: Value;
};

export default class ZodBridge<T extends ZodRawShape> extends Bridge {
schema: ZodObject<T> | ZodEffects<ZodObject<T>>;
export default class ZodBridge<T> extends Bridge {
schema: ZodType<T>;
provideDefaultLabelFromFieldName: boolean;

constructor({
schema,
provideDefaultLabelFromFieldName = true,
}: {
schema: ZodObject<T> | ZodEffects<ZodObject<T>>;
schema: ZodType<T>;
provideDefaultLabelFromFieldName?: boolean;
}) {
super();
Expand Down Expand Up @@ -108,8 +107,8 @@ export default class ZodBridge<T extends ZodRawShape> extends Bridge {
getField(name: string) {
let field: ZodType = this.schema;

if (this.schema instanceof ZodEffects) {
field = this.schema._def.schema;
while (field instanceof ZodEffects) {
field = field.innerType();
}

for (const key of joinName(null, name)) {
Expand Down
6 changes: 3 additions & 3 deletions packages/uniforms-mui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uniforms-mui",
"version": "4.0.0-beta.2",
"version": "4.0.0-beta.4",
"license": "MIT",
"main": "./cjs/index.js",
"module": "./esm/index.js",
Expand Down Expand Up @@ -30,9 +30,9 @@
"src/*.tsx"
],
"peerDependencies": {
"@mui/material": "^5.0.0",
"@mui/material": "^6.0.0",
"react": "^18.0.0 || ^17.0.0",
"uniforms": "4.0.0-beta.2"
"uniforms": "4.0.0-beta.4"
},
"dependencies": {
"invariant": "^2.0.0",
Expand Down
Loading

0 comments on commit 3d07edb

Please sign in to comment.