Skip to content

Commit

Permalink
Fix[#2183] inferFlattenedErrors should be a type about input type (#2321
Browse files Browse the repository at this point in the history
)

* Fix inferFlattenedErrors type

* Update yarn.lock

---------

Co-authored-by: Colin McDonnell <[email protected]>
  • Loading branch information
ytsunekawa and colinhacks authored May 3, 2024
1 parent adc005e commit 90efe7f
Show file tree
Hide file tree
Showing 6 changed files with 770 additions and 2,548 deletions.
4 changes: 2 additions & 2 deletions deno/lib/ZodError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TypeOf, ZodType } from "./index.ts";
import type { input, TypeOf, ZodType } from "./index.ts";
import { util } from "./helpers/index.ts";
import { Primitive } from "./helpers/typeAliases.ts";
import { ZodParsedType } from "./helpers/util.ts";
Expand All @@ -8,7 +8,7 @@ type allKeys<T> = T extends any ? keyof T : never;
export type inferFlattenedErrors<
T extends ZodType<any, any, any>,
U = string
> = typeToFlattenedError<TypeOf<T>, U>;
> = typeToFlattenedError<input<T>, U>;
export type typeToFlattenedError<T, U = string> = {
formErrors: U[];
fieldErrors: {
Expand Down
16 changes: 16 additions & 0 deletions deno/lib/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,22 @@ test("formatting with nullable and optional fields", () => {
}
});

test("inferFlattenedErrors", () => {
const schemaWithTransform = z
.object({ foo: z.string() })
.transform((o) => ({ bar: o.foo }));

const result = schemaWithTransform.safeParse({});

expect(result.success).toEqual(false);
if (!result.success) {
type ValidationErrors = z.inferFlattenedErrors<typeof schemaWithTransform>;
const error: ValidationErrors = result.error.flatten();
expect(error.formErrors).toEqual([]);
expect(error.fieldErrors).toEqual({ foo: ["Required"] });
}
});

const stringWithCustomError = z.string({
errorMap: (issue, ctx) => ({
message:
Expand Down
13 changes: 4 additions & 9 deletions deno/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -646,11 +646,14 @@ const ipv4Regex =
const ipv6Regex =
/^(([a-f0-9]{1,4}:){7}|::([a-f0-9]{1,4}:){0,6}|([a-f0-9]{1,4}:){1}:([a-f0-9]{1,4}:){0,5}|([a-f0-9]{1,4}:){2}:([a-f0-9]{1,4}:){0,4}|([a-f0-9]{1,4}:){3}:([a-f0-9]{1,4}:){0,3}|([a-f0-9]{1,4}:){4}:([a-f0-9]{1,4}:){0,2}|([a-f0-9]{1,4}:){5}:([a-f0-9]{1,4}:){0,1})([a-f0-9]{1,4}|(((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2}))\.){3}((25[0-5])|(2[0-4][0-9])|(1[0-9]{2})|([0-9]{1,2})))$/;

<<<<<<< HEAD
// https://stackoverflow.com/questions/7860392/determine-if-string-is-in-base64-using-javascript
const base64Regex =
/^([0-9a-zA-Z+/]{4})*(([0-9a-zA-Z+/]{2}==)|([0-9a-zA-Z+/]{3}=))?$/;

// based on https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
const hostnameRegex =
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;

// simple
// const dateRegexSource = `\\d{4}-\\d{2}-\\d{2}`;
// no leap year validation
Expand All @@ -663,14 +666,6 @@ function timeRegexSource(args: { precision?: number | null }) {
// let regex = `\\d{2}:\\d{2}:\\d{2}`;
let regex = `([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d`;

=======
// based on https://stackoverflow.com/questions/106179/regular-expression-to-match-dns-hostname-or-ip-address
const hostnameRegex =
/^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)+([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$/;

// Adapted from https://stackoverflow.com/a/3143231
const datetimeRegex = (args: { precision: number | null; offset: boolean }) => {
>>>>>>> a40e657 (fix: make URL validation more strict, allow only URLs with valid hostname and forbid TLD without subdomains)
if (args.precision) {
regex = `${regex}\\.\\d{${args.precision}}`;
} else if (args.precision == null) {
Expand Down
4 changes: 2 additions & 2 deletions src/ZodError.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TypeOf, ZodType } from ".";
import type { input, TypeOf, ZodType } from ".";
import { util } from "./helpers";
import { Primitive } from "./helpers/typeAliases";
import { ZodParsedType } from "./helpers/util";
Expand All @@ -8,7 +8,7 @@ type allKeys<T> = T extends any ? keyof T : never;
export type inferFlattenedErrors<
T extends ZodType<any, any, any>,
U = string
> = typeToFlattenedError<TypeOf<T>, U>;
> = typeToFlattenedError<input<T>, U>;
export type typeToFlattenedError<T, U = string> = {
formErrors: U[];
fieldErrors: {
Expand Down
16 changes: 16 additions & 0 deletions src/__tests__/error.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,22 @@ test("formatting with nullable and optional fields", () => {
}
});

test("inferFlattenedErrors", () => {
const schemaWithTransform = z
.object({ foo: z.string() })
.transform((o) => ({ bar: o.foo }));

const result = schemaWithTransform.safeParse({});

expect(result.success).toEqual(false);
if (!result.success) {
type ValidationErrors = z.inferFlattenedErrors<typeof schemaWithTransform>;
const error: ValidationErrors = result.error.flatten();
expect(error.formErrors).toEqual([]);
expect(error.fieldErrors).toEqual({ foo: ["Required"] });
}
});

const stringWithCustomError = z.string({
errorMap: (issue, ctx) => ({
message:
Expand Down
Loading

0 comments on commit 90efe7f

Please sign in to comment.