{"id":31070888,"url":"https://github.com/4nikerin/zod-valid","last_synced_at":"2026-05-15T22:35:24.480Z","repository":{"id":312893921,"uuid":"1046853714","full_name":"4nikerin/zod-valid","owner":"4nikerin","description":"A tiny Zod helper library to safely normalize values to valid types with flexible handling of null, undefined, and fallback values. Supports strings, numbers, booleans, enums, and arrays.","archived":false,"fork":false,"pushed_at":"2025-10-05T23:00:43.000Z","size":221,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-10-06T01:06:15.829Z","etag":null,"topics":["api-validation","data-validation","schema","schema-validation","server-data","typescript","validation","validation-library","zod","zod-schema","zod-utils"],"latest_commit_sha":null,"homepage":"https://www.npmjs.com/package/zod-valid","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4nikerin.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-08-29T10:31:25.000Z","updated_at":"2025-10-05T23:00:27.000Z","dependencies_parsed_at":"2025-10-06T01:05:49.435Z","dependency_job_id":"ab6a73ea-0bd4-4ef9-860c-c66321253e15","html_url":"https://github.com/4nikerin/zod-valid","commit_stats":null,"previous_names":["4nikerin/zod-valid"],"tags_count":7,"template":false,"template_full_name":null,"purl":"pkg:github/4nikerin/zod-valid","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4nikerin%2Fzod-valid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4nikerin%2Fzod-valid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4nikerin%2Fzod-valid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4nikerin%2Fzod-valid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4nikerin","download_url":"https://codeload.github.com/4nikerin/zod-valid/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4nikerin%2Fzod-valid/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33082075,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-15T20:25:35.270Z","status":"ssl_error","status_checked_at":"2026-05-15T20:25:34.732Z","response_time":103,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.5:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["api-validation","data-validation","schema","schema-validation","server-data","typescript","validation","validation-library","zod","zod-schema","zod-utils"],"created_at":"2025-09-15T23:03:33.405Z","updated_at":"2026-05-15T22:35:24.463Z","avatar_url":"https://github.com/4nikerin.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ch1 align=\"center\"\u003ezod-valid\u003c/h1\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/zod-valid\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/zod-valid.svg\" alt=\"npm version\"/\u003e\n  \u003c/a\u003e\n  \u003ca href=\"LICENSE\"\u003e\n    \u003cimg src=\"https://img.shields.io/badge/License-MIT-green.svg\" alt=\"License\"/\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\nA tiny helper library for [Zod](https://zod.dev) to safely normalize values to valid types with flexible handling of `null`, `undefined`, and fallback values.\nSupports strings, numbers, booleans, ISO dates, enums, arrays and objects.\n\nIt is very important to validate data coming from the server on the client side, making it more resilient to various errors, including critical ones.\n\n## Table of Contents\n\n- [Features](#features)\n- [Installation](#installation)\n- [Usage](#usage)\n  - [toValidString](#tovalidstring)\n  - [toValidNumber](#tovalidnumber)\n  - [toValidISO](#tovalidiso)\n  - [toValidBoolean](#tovalidboolean)\n  - [toValidArray](#tovalidarray)\n  - [toValidObject](#tovalidobject)\n- [Utils](#utils)\n  - [nonNullable](#nonnullable)\n- [Examples](#examples)\n- [License](#license)\n\n## Features\n\n- Coerce any value to a valid type.\n- Fine-grained control over empty values (`null` / `undefined`):\n  - `none` — disallow both.\n  - `optional` — allow only `undefined`.\n  - `nullable` — allow only `null`.\n  - `nullish` — allow both.\n- Optionally replace empty values with a `fallback`.\n- Works with custom base schemas (`z.string().min(3)` etc.).\n- Includes helpers: `toValidString`, `toValidNumber`, `toValidBoolean`, `toValidISO`, `toValidArray`.\n\n## Installation\n\n```bash\nnpm install zod-valid zod\n```\n\n## Usage\n\n### toValidString\n\nCreates a Zod schema that coerces any value to a string and provides flexible handling of empty values (`null` / `undefined`) and invalid inputs.\n\nUsage forms:\n- `toValidString(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidString(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- Converts any non-empty input to a string using `String(val)`.\n- Allowed empty values (`null` / `undefined`) are controlled via allow and may be preserved or replaced with fallback.\n\nBehavior options:\n| Option    | Description                                                                                                         | Default        | Required |\n|-----------|---------------------------------------------------------------------------------------------------------------------|----------------|----------|\n| `type`    | Base Zod schema to apply.                                                                                           | `z.string()`   | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).                          | `null`         | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"`    | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`.        | `true`         | No       |\n\nExamples:\n```ts\nimport { toValidString } from \"zod-valid\";\n\nconst schema = toValidString();\nschema.parse(\"abc\");                  // \"abc\"\nschema.parse(123);                    // \"123\"\nschema.parse(null);                   // null (default allow=\"nullish\", preserve=true)\n\nconst schema = toValidString({ allow: \"optional\" });\nschema.parse(null);                   // \"null\" (null is coerced to string)\nschema.parse(undefined);              // undefined\n\nconst schema = toValidString(z.string().min(2), { fallback: \"N/A\", preserve: false });\nschema.parse(\"a\");                    // \"N/A\"\nschema.parse(\"abc\");                  // \"abc\"\nschema.parse(null);                   // \"N/A\"\n\nconst schema = toValidString({ type: z.email(), fallback: \"empty\" });\nschema.parse(\"example@hostname.com\"); // \"example@hostname.com\"\nschema.parse(\"oops\");                 // \"empty\"\nschema.parse(null);                   // null\nschema.parse(undefined);              // undefined\n```\n\n### toValidNumber\n\nCreates a Zod schema that coerces input values to numbers and provides flexible handling of empty values (`null` / `undefined`) and invalid inputs.\n\nUsage forms:\n- `toValidNumber(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidNumber(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- Converts values to numbers using `Number(val)`.\n- Non-numeric or invalid values are replaced with `fallback` depending on `allow` and `preserve`.\n\nBehavior options:\n| Option    | Description                                                                                                         | Default        | Required |\n|-----------|---------------------------------------------------------------------------------------------------------------------|----------------|----------|\n| `type`    | Base Zod schema to apply.                                                                                           | `z.number()`   | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).                          | `null`         | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"`    | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`.        | `true`         | No       |\n\nExamples:\n```ts\nimport { toValidNumber } from \"zod-valid\";\n\nconst schema = toValidNumber();\nschema.parse(42);        // 42\nschema.parse(\"123\");     // 123\nschema.parse(null);      // null (default allow=\"nullish\", preserve=true)\n\nconst schema = toValidNumber({ allow: \"optional\" });\nschema.parse(null);      // null replaced with \"null\" logic → returns fallback if preserve=false\nschema.parse(undefined); // undefined\n\nconst schemaTyped = toValidNumber(z.number().min(10), { fallback: 0 });\nschemaTyped.parse(5);    // 0 (invalid, replaced with fallback)\nschemaTyped.parse(20);   // 20\n\nconst schema = toValidNumber({ allow: \"nullish\", fallback: 99, preserve: false });\nschema.parse(\"abc\");     // 99 (invalid string → fallback)\nschema.parse(null);      // 99\nschema.parse(undefined); // 99\n```\n\n### toValidISO\n\nCreates a Zod schema that coerces input to ISO 8601 date strings and provides flexible handling of empty values (`null` / `undefined`) and invalid inputs.\n\nUsage forms:\n- `toValidISO(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidISO(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- Converts valid string inputs to `ISO 8601` dates.\n- Non-string or invalid values are replaced with `fallback` depending on `allow` and `preserve`.\n\nBehavior options:\n| Option    | Description                                                                                                         | Default             | Required |\n|-----------|---------------------------------------------------------------------------------------------------------------------|--------------------|----------|\n| `type`    | Base Zod schema to apply.                                                                                           | `z.iso.datetime()` | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).                          | `null`             | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"`        | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`.        | `true`             | No       |\n\nExamples:\n```ts\nimport { toValidISO } from \"zod-valid\";\n\nconst schema = toValidISO();\nschema.parse(\"2025-09-02 10:00\"); // \"2025-09-02T10:00:00Z\"\nschema.parse(null);               // null (default allow=\"nullish\", preserve=true)\n\nconst schema = toValidISO({ allow: \"optional\" });\nschema.parse(null);               // null (default fallback)\nschema.parse(undefined);          // undefined\n\nconst schemaTyped = toValidISO(z.string().datetime(), { fallback: \"1970-01-01T00:00:00Z\" });\nschemaTyped.parse(\"invalid\");     // \"1970-01-01T00:00:00Z\"\nschemaTyped.parse(\"2025-09-11\");  // \"2025-09-11T00:00:00.000Z\"\n\nconst schema = toValidISO({ allow: \"nullable\", fallback: \"N/A\", preserve: false });\nschemaFallback.parse(\"abc\");      // \"N/A\"\nschemaFallback.parse(null);       // \"N/A\"\nschemaFallback.parse(undefined);  // \"N/A\"\n```\n\n### toValidBoolean\n\nCreates a Zod schema that coerces input to boolean values and provides flexible handling of empty values (`null` / `undefined`) and invalid inputs.\n\nUsage forms:\n- `toValidBoolean(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidBoolean(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- Converts values to boolean (\"true\"/\"false\", numbers, objects).\n- Allowed empty values (`null` / `undefined`) are controlled via allow and may be preserved or replaced.\n- Other invalid values are coerced to `boolean` following the above rules.\n\nBehavior options:\n| Option    | Description                                                                                                         | Default        | Required |\n|-----------|---------------------------------------------------------------------------------------------------------------------|----------------|----------|\n| `type`    | Base Zod schema to apply.                                                                                           | `z.boolean()`  | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).                          | `null`         | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"`    | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`.        | `true`         | No       |\n\nExamples:\n```ts\nimport { toValidBoolean } from \"zod-valid\";\n\nconst schema = toValidBoolean();\nschema.parse(\"true\");            // true\nschema.parse(\"FALSE\");           // false\nschema.parse(1);                 // true\nschema.parse(0);                 // false\nschema.parse(null);              // null (default allow=\"nullish\", preserve=true)\n\nconst schema = toValidBoolean({ allow: \"optional\" });\nschema.parse(null);              // false\nschema.parse(undefined);         // undefined\n\nconst schemaTyped = toValidBoolean(z.boolean(), { fallback: false });\nschemaTyped.parse(\"true\");       // true\nschemaTyped.parse(\"oops\");       // false\n\nconst schemaFallback = toValidBoolean({ allow: \"nullish\", fallback: false, preserve: false });\nschemaFallback.parse(null);      // false\nschemaFallback.parse(undefined); // false\nschemaFallback.parse(\"oops\");    // false\n```\n\n### toValidArray\n\nCreates a Zod schema that ensures input is an array of elements matching `type`, with flexible handling of empty (`null`/`undefined`) and invalid values.\n\nUsage forms:\n- `toValidArray(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidArray(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- If input is not an array or is an invalid value, it is replaced with `fallback` (depending on `allow` and `preserve`).\n- Allowed empty values (`null` / `undefined`) are controlled via `allow` and may be preserved or replaced.\n- If `strict: true`, removes any elements that do **not** pass the `type` schema (invalid elements), as well as `null` and `undefined`, **but does not remove fallback values** if they are not `null` or `undefined`.\n\nBehavior options:\n| Option    | Description                                                                                               | Default            | Required |\n|-----------|-----------------------------------------------------------------------------------------------------------|------------------|----------|\n| `type`    | Zod schema for array elements. Can be passed either as the first argument (`toValidArray(z.string())`) or inside the options object (`toValidArray({ type: z.string() })`). | `z.array(z.never())` | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).               | `[]`             | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"`        | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`. | `true`             | No       |\n| `strict`  | Whether to strictly enforce the element schema:\u003cbr\u003e- `true` — removes any elements that do **not** pass the `type` schema (invalid elements).\u003cbr\u003eAlso removes `null` and `undefined` values, even if allowed by `allow`.\u003cbr\u003e**Does not remove fallback values** if they are not `null` or `undefined`.\u003cbr\u003e- `false` — invalid elements and allowed empty values are preserved (or replaced by `fallback` if `preserve: false`). | `true`           | No       |\n\nExamples:\n```ts\nimport { toValidArray } from \"zod-valid\";\n\nconst schema = toValidArray(z.string());\nschema.parse([\"a\", \"b\"]);              // [\"a\", \"b\"]\nschema.parse(null);                    // null (allow=\"nullish\", preserve=true)\n\nconst schemaTyped = toValidArray(z.number(), { allow: \"optional\" });\nschemaTyped.parse([1, 2]);             // [1, 2]\nschemaTyped.parse(undefined);          // undefined\n\nconst schema3 = toValidArray({ type: z.number(), allow: \"nullable\", fallback: [], preserve: false });\nschema3.parse(\"oops\");                 // []\nschema3.parse(null);                   // []\n\nconst strictSchema = toValidArray({ type: z.number(), strict: true });\nstrictSchema.parse([1, \"x\", 2, null]); // [1, 2] — removes invalid values, null, and undefined; preserves fallback values\n```\n\n### toValidObject\n\nCreates a Zod schema that ensures the input is a plain object (validated by `type`), with flexible handling of empty values (`null` / `undefined`) and a fallback value for invalid cases.\n\nUsage forms:\n- `toValidObject(type, options?)` — pass the base Zod schema as the first argument and options as the second.\n- `toValidObject(options)` — pass only an options object, where options.type defines the base schema.\n\nBehavior:\n- If the input is a plain object ({}), it is validated against type.\n- Non-object or invalid values are replaced with fallback depending on `allow` and `preserve`.\n\nBehavior options:\n| Option    | Description                                                                                  | Default                                | Required |\n|-----------|----------------------------------------------------------------------------------------------|----------------------------------------|----------|\n| `type`    | Base Zod schema for validating objects.                                                      | `z.object({}).catchall(z.any())` (any plain object) | No       |\n| `fallback`| Value returned instead of invalid input or when replacing empty input (`preserve: false`).   | `null`                                 | No       |\n| `allow`   | Defines which empty values are considered valid:\u003cbr\u003e- `\"none\"` — neither `null` nor `undefined` allowed.\u003cbr\u003e- `\"optional\"` — only `undefined` allowed.\u003cbr\u003e- `\"nullable\"` — only `null` allowed.\u003cbr\u003e- `\"nullish\"` — both `null` and `undefined` allowed. | `\"nullish\"` | No       |\n| `preserve`| Behavior for allowed empty values:\u003cbr\u003e- `true` — return them as-is.\u003cbr\u003e- `false` — replace with `fallback`. | `true` | No       |\n\nExamples:\n```ts\nimport { toValidArray } from \"zod-valid\";\n\nconst schemaNum = toValidObject({ type: z.object({ x: z.number() }) });\nschemaNum.parse({ x: 42 });        // { x: 42 }\nschemaNum.parse({ x: \"oops\" });    // null (invalid, replaced with fallback)\n\nconst schemaStrict = toValidObject(z.object({ id: z.string() }), { allow: \"nullish\" });\nschemaStrict.parse({ id: \"abc\" }); // { id: \"abc\" }\nschemaStrict.parse({ id: 123 });   // null (invalid, fallback applied)\n\nconst RoleEnum = z.enum([\"admin\", \"user\", \"guest\"]);\nconst schemaEnum = toValidObject({ type: RoleEnum, allow: \"optional\" });\nschemaEnum.parse(\"admin\");         // \"admin\"\nschemaEnum.parse(\"superuser\");     // null (invalid value -\u003e fallback)\nschemaEnum.parse(null);            // null (replaced with fallback, since null not allowed)\nschemaEnum.parse(undefined);       // undefined\n\nconst schemaReplace = toValidObject({ type: z.object({}), allow: \"nullish\", fallback: {}, preserve: false });\nschemaReplace.parse(null);         // {} (null allowed, but replaced with fallback)\nschemaReplace.parse(undefined);    // {} (undefined allowed, but replaced with fallback)\nschemaReplace.parse(\"oops\");       // {} (invalid, replaced with fallback)\n```\n\n## Utils\n\n### nonNullable\n\nYou can remove `null` and `undefined` from the result of an API call using the `nonNullable` utility. This utility adds a `z.transform` to the schema, removing `null` and `undefined` from the type and returning a custom error.\n\nBehavior options:\n| Option   | Description        | Default                        | Required |\n|----------|------------------|--------------------------------|----------|\n| `schema` | A Zod schema       | —                              | Yes      |\n| `message`| Error message text | `\"Cannot be null or undefined\"` | No       |\n\nExamples:\n```ts\nimport z from \"zod\";\nimport { toValidString, toValidNumber } from \"zod-valid\";\nimport { nonNullable } from \"zod-valid/utils\";\n\nconst emailSchema = z.object({\n  id: toValidNumber({ allow: \"none\", preserve: false }),\n  name: toValidString(),\n  surname: toValidString(),\n  address: toValidString(),\n});\n\n/*\n  {\n    id: number;v\n    name?: string | null | undefined;\n    surname?: string | null | undefined;\n    address?: string | null | undefined;\n  }\n*/\ntype Email = z.infer\u003ctypeof emailSchema\u003e;\n\nconst formSchema = z.object({\n  name: nonNullable(emailSchema.shape.name).refine((v) =\u003e v, \"Name is required\"),\n  surname: emailSchema.shape.name.nonoptional(),\n  address: nonNullable(emailSchema.shape.name).optional(),\n});\n\n/*\n  {\n    name: string;\n    surname: string | null;\n    address?: string | undefined;\n  }\n*/\ntype Form = z.infer\u003ctypeof formSchema\u003e;\n\n```\n\n## Examples\n\nℹ️ You can find all possible usage examples for each function in the [tests folder](./tests).\n\n```ts\nimport { z } from \"zod\";\nimport {\n  toValidNumber,\n  toValidString,\n  toValidISO,\n  toValidArray,\n  toValidBoolean,\n  toValidObject,\n} from \"zod-valid\";\n\nconst ResponseSchema = toValidObject({\n  type: z.object({\n    users: toValidArray({\n      type: toValidObject({\n        type: z.object({\n          id: toValidNumber({ allow: \"none\" }),\n          name: toValidString(),\n          email: toValidString({ type: z.email(), fallback: \"N/A\", preserve: false }),\n          isActive: toValidBoolean(),\n          createdAt: toValidISO(),\n        }),\n      }),\n      preserve: false,\n    })\n  }),\n});\n\n// or more shorter\n\nconst ResponseSchema = toValidObject(z.object({\n  users: toValidArray(\n    toValidObject(z.object({\n      id: toValidNumber({ allow: \"none\" }),\n      name: toValidString(),\n      email: toValidString(z.email(), { fallback: \"N/A\", preserve: false }),\n      isActive: toValidBoolean(),\n      createdAt: toValidISO(),\n    })),\n    {\n      preserve: false,\n    },\n  )\n}));\n\n/*\n  type ResponseType = {\n    users: {\n      id: number;\n      name?: string | null | undefined;\n      email: string;\n      isActive?: boolean | null | undefined;\n      createdAt?: string | null | undefined;\n    }[]\n  } | null | undefined\n*/\ntype ResponseType = z.infer\u003ctypeof ResponseSchema\u003e\n\nasync function getUser() {\n  const response = await fetch(\"/api/users\");\n  const rawData = await response.json();\n  const validData = ResponseSchema.parse(rawData);\n  return validData;\n}\n```\n\n## License\n\nMIT © 2025 Nikerin Evgenii \u003c4nikerin@gmail.com\u003e (https://github.com/4nikerin)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4nikerin%2Fzod-valid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4nikerin%2Fzod-valid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4nikerin%2Fzod-valid/lists"}