-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(extend-schema): better schema callback with the initial schema f…
…or extension (#122) * feat(extendable-schema): add combinator for user & initial schema * configure datefield * configure arrayField * configure string, booleans, digit * story examples * listfield with extendable schema * fix stories * fix nesting errors * update rejdme
- Loading branch information
1 parent
587ec5b
commit 44aa7ac
Showing
39 changed files
with
476 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,39 @@ | ||
import { ArrayCardinality, ZodAny, ZodArray, ZodSchema, z } from "zod"; | ||
import type { ArrayCardinality, ZodAny, ZodArray, ZodSchema } from "zod"; | ||
import { z } from "zod"; | ||
|
||
import { | ||
ZodField, | ||
ZodFieldConfig, | ||
ZodParams, | ||
defaultParams, | ||
zodField, | ||
} from "../zod-field"; | ||
import { prepareSchema } from "../../utils"; | ||
import { FieldConfig } from "../field"; | ||
import { type ZodField, defaultParams, zodField } from "../zod-field"; | ||
|
||
export type ZodArrayField<Element extends ZodSchema = ZodAny> = ZodField< | ||
ZodArray<Element, ArrayCardinality>, | ||
ZodArray<Element, ArrayCardinality> | ||
>; | ||
|
||
export type ArrayFieldParams<ElementSchema extends z.Schema> = Partial< | ||
ZodFieldConfig< | ||
ZodArray<ElementSchema, "atleastone">, | ||
ZodArray<ElementSchema, "many"> | ||
> | ||
> & | ||
ZodParams; | ||
export type ArrayFieldParams<ElementSchema extends z.Schema> = FieldConfig< | ||
ZodArray<ElementSchema, "atleastone">, | ||
ZodArray<ElementSchema, "many"> | ||
>; | ||
|
||
export const arrayField = <ElementSchema extends z.Schema>({ | ||
required_error = defaultParams.required_error, | ||
value = [], | ||
elementSchema, | ||
schema, | ||
optionalSchema, | ||
...config | ||
}: { elementSchema: ElementSchema } & ArrayFieldParams<ElementSchema>) => | ||
zodField({ | ||
value, | ||
schema: z.array(elementSchema).nonempty(required_error), | ||
optionalSchema: z.array(elementSchema), | ||
...prepareSchema({ | ||
initial: { | ||
schema: z.array(elementSchema).nonempty(required_error), | ||
optionalSchema: z.array(elementSchema), | ||
}, | ||
user: { | ||
schema, | ||
optionalSchema, | ||
}, | ||
}), | ||
...config, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { act, renderHook } from "@testing-library/react"; | ||
import { useFieldActions, useFieldErrors } from "form-atoms"; | ||
import { describe, expect, it } from "vitest"; | ||
|
||
import { dateField } from "./dateField"; | ||
|
||
describe("dateField()", () => { | ||
describe("schema", () => { | ||
it("extends the internal schema", async () => { | ||
const field = dateField({ | ||
value: new Date("2000-01-01"), | ||
schema: (s) => | ||
s.max(new Date("1999-12-31"), { | ||
message: "Date can't be in the 21st century", | ||
}), | ||
}); | ||
|
||
const { result: actions } = renderHook(() => useFieldActions(field)); | ||
const { result: errors } = renderHook(() => useFieldErrors(field)); | ||
|
||
await act(async () => actions.current.validate()); | ||
expect(errors.current).toEqual(["Date can't be in the 21st century"]); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.