{"id":27933217,"url":"https://github.com/thonymg/maybe-zod","last_synced_at":"2025-08-19T23:05:05.222Z","repository":{"id":285658981,"uuid":"958444042","full_name":"thonymg/maybe-zod","owner":"thonymg","description":"TypeScript utility library that combines Zod schema validation with error handling using a Maybe monad pattern for negative space programming.","archived":false,"fork":false,"pushed_at":"2025-04-07T08:15:57.000Z","size":33,"stargazers_count":3,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-08-09T10:43:07.022Z","etag":null,"topics":["maybe","monad","typescript","zod"],"latest_commit_sha":null,"homepage":"","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/thonymg.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}},"created_at":"2025-04-01T07:57:07.000Z","updated_at":"2025-04-11T09:43:35.000Z","dependencies_parsed_at":null,"dependency_job_id":"bc580a0f-0644-42eb-8cf7-7005c85f7739","html_url":"https://github.com/thonymg/maybe-zod","commit_stats":null,"previous_names":["thonymg/maybe-zod"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/thonymg/maybe-zod","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thonymg%2Fmaybe-zod","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thonymg%2Fmaybe-zod/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thonymg%2Fmaybe-zod/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thonymg%2Fmaybe-zod/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thonymg","download_url":"https://codeload.github.com/thonymg/maybe-zod/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thonymg%2Fmaybe-zod/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271236280,"owners_count":24723978,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-19T02:00:09.176Z","response_time":63,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["maybe","monad","typescript","zod"],"created_at":"2025-05-07T04:55:30.047Z","updated_at":"2025-08-19T23:05:05.199Z","avatar_url":"https://github.com/thonymg.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Maybe-Zod\n\nA lightweight TypeScript utility library that combines Zod schema validation with error handling using a Maybe monad pattern. This library provides a clean and type-safe way to validate data and handle errors without try-catch blocks.\n\n## Features\n\n- Type-safe validation using Zod schemas\n- Synchronous and asynchronous validation support\n- Clean error handling without try-catch blocks\n- Zero dependencies (except for Zod)\n- Full TypeScript support with type inference\n- Comprehensive test coverage\n- Support for complex validation scenarios\n- Transformation of validated data\n- Detailed error messages\n\n## Installation\n\n```bash\n# Using npm\nnpm install maybe-zod\n\n# Using yarn\nyarn add maybe-zod\n\n# Using bun\nbun add maybe-zod\n```\n\n## Usage\n\n### Basic Example\n\n```typescript\nimport { z } from \"zod\";\nimport { Maybe } from \"maybe-zod\";\n\n// Define your schema\nconst userSchema = z.object({\n  name: z.string().min(2).max(50),\n  age: z.number().int().positive(),\n  email: z.string().email(),\n});\n\ntype User = z.infer\u003ctypeof userSchema\u003e;\n// Define your Output schema\ntype UserOutput = User \u0026\u0026 { displayName: string }\n\n// Define your processing function\nconst processUser = (user: z.infer\u003ctypeof userSchema\u003e) =\u003e ({\n  ...user,\n  displayName: `${user.name} (${user.age})`\n});\n\n// Create a validated processor\nconst validateUser = Maybe\u003cUser, UserOutput\u003e(processUser, userSchema);\n\n// Use it with valid data\nconst [error, result] = validateUser({\n  name: \"Alice\",\n  age: 30,\n  email: \"alice@example.com\"\n});\n// error = null\n// result = { name: \"Alice\", age: 30, email: \"alice@example.com\", displayName: \"Alice (30)\" }\n\n// Use it with invalid data\nconst [error, result] = validateUser({\n  name: \"A\",\n  age: -5,\n  email: \"invalid\"\n});\n// error contains validation messages:\n// - \"String must contain at least 2 character(s)\"\n// - \"Number must be greater than 0\"\n// - \"Invalid email\"\n// result = null\n```\n\n### Advanced Examples\n\n#### 1. Complex Nested Objects\n\n```typescript\nconst advancedSchema = z.object({\n  id: z.string().uuid(),\n  metadata: z.object({\n    tags: z.array(z.string()).min(1),\n    createdAt: z.date()\n  })\n});\n\n\ntype Advanced = z.infer\u003ctypeof advancedSchema\u003e;\n// Define your Output schema\ntype AdvancedOutput = { id: string }\n\nconst processData = (data: Advanced) =\u003e data.id;\nconst validate = Maybe\u003cAdvanced, AdvancedOutput\u003e(processData, advancedSchema);\n\nconst [error, result] = validate({\n  id: '123e4567-e89b-12d3-a456-426614174000',\n  metadata: {\n    tags: ['important', 'urgent'],\n    createdAt: new Date()\n  }\n});\n```\n\n#### 2. Optional Fields and Discriminated Unions\n\n```typescript\n// Optional fields\nconst optionalSchema = z.object({\n  title: z.string(),\n  description: z.string().optional(),\n  tags: z.array(z.string()).optional()\n});\n\n// Discriminated unions\nconst resultSchema = z.discriminatedUnion('type', [\n  z.object({ type: z.literal('success'), value: z.number() }),\n  z.object({ type: z.literal('error'), message: z.string() })\n]);\n\ntype OptionalS = z.infer\u003ctypeof optionalSchema\u003e;\n// Define your Output schema\ntype ResultS = z.infer\u003ctypeof resultSchema\u003e;\n\n\nconst validateResult = Maybe\u003cOptionalS, ResultS\u003e(\n  (data) =\u003e data.type === 'success' ? data.value : 0,\n  resultSchema\n);\n\nconst [error, result] = validateResult({ type: 'success', value: 42 });\n// result = 42\n```\n\n#### 3. Array Transformations\n\n```typescript\nconst numberArraySchema = z.array(z.number());\nconst sum = (numbers: number[]) =\u003e numbers.reduce((a, b) =\u003e a + b, 0);\nconst validateSum = Maybe\u003cnumber[], number\u003e(sum, numberArraySchema);\n\nconst [error, result] = validateSum([1, 2, 3, 4, 5]);\n// result = 15\n```\n\n### Async Validation\n\n```typescript\nimport { AsyncMaybe } from \"maybe-zod\";\n\nconst validateUserAsync = AsyncMaybe(processUser, userSchema);\n\n// Basic async usage\nconst [error, result] = await validateUserAsync(\n  Promise.resolve({\n    name: \"Alice\",\n    age: 30,\n    email: \"alice@example.com\"\n  })\n);\n\n// Error handling for rejected promises\ntry {\n  const [error, result] = await validateUserAsync(\n    Promise.reject(new Error('Network error'))\n  );\n  // error = 'Unknown error'\n  // result = null\n} catch {\n  // Handle any unexpected errors\n}\n```\n\n## API Reference\n\n### `Maybe\u003cT, U\u003e`\n\n```typescript\nMaybe\u003cT, U\u003e(\n  fn: (params: T) =\u003e U,\n  schema: z.ZodSchema\u003cT\u003e\n) =\u003e (data: T) =\u003e [string | null, U | null]\n```\n\nCreates a validation wrapper for synchronous data processing.\n\n#### Parameters:\n- `fn`: A function that processes the validated data\n- `schema`: A Zod schema that defines the shape and validation rules for the data\n- Returns a function that takes input data and returns a tuple of [error, result]\n\n### `AsyncMaybe\u003cT, U\u003e`\n\n```typescript\nAsyncMaybe\u003cT, U\u003e(\n  fn: (params: T) =\u003e U,\n  schema: z.ZodSchema\u003cT\u003e\n) =\u003e (data: Promise\u003cT\u003e) =\u003e Promise\u003c[string | null, U | null]\u003e\n```\n\nCreates a validation wrapper for asynchronous data processing.\n\n#### Parameters:\n- `fn`: A function that processes the validated data\n- `schema`: A Zod schema that defines the shape and validation rules for the data\n- Returns a function that takes a Promise of input data and returns a Promise of [error, result]\n\n## Error Handling\n\nThe library returns errors in a structured format:\n- For validation errors, the error message contains a JSON string of all validation failures\n- For async operations, unknown errors are caught and returned as 'Unknown error'\n- The result is always null when an error occurs\n\n## Best Practices\n\n1. **Type Safety**\n   - Always define your schemas with proper types\n   - Use `z.infer\u003ctypeof schema\u003e` for type inference\n\n2. **Error Handling**\n   - Always check the error value before using the result\n   - Handle both validation errors and unknown errors in async operations\n\n3. **Schema Design**\n   - Keep schemas modular and reusable\n   - Use schema composition for complex validations\n\n## Testing\n\nThe library includes comprehensive tests. Run them using:\n\n```bash\nbun test\n```\n\nTest coverage includes:\n- Basic validations\n- Complex object validations\n- Array transformations\n- Async operations\n- Error cases\n- Edge cases\n\n\n\n## ❤️ Contributors\n\nThis project was initiated and is maintained by [ThonyMg](https://github.com/ThonyMg). I am available for freelance work on Svelte and VueJs projects. Feel free to reach out through my [Linkedin profile](https://www.linkedin.com/company/105997457/) for collaboration opportunities.\n\n## ⚖️ License\n\nThis project is licensed under the MIT License. For more details, see the [LICENSE](https://github.com/thonymg/maybe-zod/blob/master/license) file.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthonymg%2Fmaybe-zod","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthonymg%2Fmaybe-zod","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthonymg%2Fmaybe-zod/lists"}