{"id":26954621,"url":"https://github.com/evandersondev/zard","last_synced_at":"2025-04-03T02:18:34.692Z","repository":{"id":282776442,"uuid":"949633363","full_name":"evandersondev/zard","owner":"evandersondev","description":null,"archived":false,"fork":false,"pushed_at":"2025-03-16T21:56:38.000Z","size":0,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-16T22:17:31.623Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Dart","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/evandersondev.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","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}},"created_at":"2025-03-16T21:47:15.000Z","updated_at":"2025-03-16T21:56:41.000Z","dependencies_parsed_at":"2025-03-16T22:27:33.320Z","dependency_job_id":null,"html_url":"https://github.com/evandersondev/zard","commit_stats":null,"previous_names":["evandersondev/zard"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evandersondev%2Fzard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evandersondev%2Fzard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evandersondev%2Fzard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/evandersondev%2Fzard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/evandersondev","download_url":"https://codeload.github.com/evandersondev/zard/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922229,"owners_count":20855345,"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","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":[],"created_at":"2025-04-03T02:18:34.078Z","updated_at":"2025-04-03T02:18:34.674Z","avatar_url":"https://github.com/evandersondev.png","language":"Dart","funding_links":["https://buymeacoffee.com/evandersondev"],"categories":[],"sub_categories":[],"readme":"# Zard 🧩\n\nZard is a schema validation and transformation library for Dart, inspired by the popular [Zod](https://github.com/colinhacks/zod) library for JavaScript. With Zard, you can define schemas to validate and transform data easily and intuitively.\n\n\u003cbr\u003e\n\n### Support 💖\n\nIf you find Zard useful, please consider supporting its development 🌟 [Buy Me a Coffee](https://buymeacoffee.com/evandersondev) 🌟. Your support helps us improve the framework and make it even better!\n\n\u003cbr\u003e\n\n## Installation 📦\n\nAdd the following line to your `pubspec.yaml`:\n\n```yaml\ndependencies:\n  zard: ^0.0.5\n```\n\nThen, run:\n\n```sh\nflutter pub get\n```\n\nOr run:\n\n```sh\ndart pub add zard\n```\n\n\u003cbr\u003e\n\n## Usage 🚀\n\nZard allows you to define schemas for various data types. Below are several examples of how to use Zard, including handling errors either by using `parse` (which throws errors) or `safeParse` (which returns a success flag and error details).\n\n\u003cbr\u003e\n\n### Defining Schemas\n\n#### String Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // String validations with minimum and maximum length and email format check.\n  final schema = z.string().min(3).max(10).email(message: \"Invalid email address\");\n\n  // Using parse (throws if there is an error)\n  try {\n    final result = schema.parse(\"example@example.com\");\n    print(\"Parsed Value: $result\"); // example@example.com\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  // Using safeParse (doesn't throw; returns error info in result object)\n  final safeResult = schema.safeParse(\"Hi\"); // \"Hi\" is too short\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\")); // Output error messages 😱\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n#### Int Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // Integer validations with minimum and maximum checks.\n  final schema = z.int().min(1).max(100);\n\n  // Using parse\n  try {\n    final result = schema.parse(50);\n    print(\"Parsed Value: $result\"); // 50\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  // Using safeParse with error handling\n  final safeResult = schema.safeParse(5); // example: if 5 is below the minimum, it returns errors\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\")); // Output error messages\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n#### Double Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // Double validations with minimum and maximum checks.\n  final schema = z.doubleType().min(1.0).max(100.0);\n\n  try {\n    final result = schema.parse(50.5);\n    print(\"Parsed Value: $result\"); // 50.5\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  final safeResult = schema.safeParse(0.5);\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\")); // Outputs error message if invalid\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n#### Boolean Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // Boolean validations\n  final schema = z.boolean();\n\n  try {\n    final result = schema.parse(true);\n    print(\"Parsed Value: $result\"); // true\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  final safeResult = schema.safeParse(false);\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\"));\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n#### List Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // List validations with inner string schema validations.\n  final schema = z.list(z.string().min(3));\n\n  try {\n    final result = schema.parse([\"abc\", \"def\"]);\n    print(\"Parsed Value: $result\"); // [abc, def]\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  final safeResult = schema.safeParse([\"ab\", \"def\"]); // \"ab\" is too short\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\"));\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n#### Map Example\n\n```dart\nimport 'package:zard/zard.dart';\n\nvoid main() {\n  // Map validations combining multiple schemas\n  final schema = z.map({\n    'name': z.string().min(3).nullable(),\n    'age': z.int().min(1).nullable(),\n  });\n\n  try {\n    final result = schema.parse({\n      'name': 'John Doe',\n      'age': 30,\n    });\n    print(\"Parsed Value: $result\"); // {name: John Doe, age: 30}\n  } catch (e) {\n    print(\"Errors (parse): ${schema.getErrors()}\");\n  }\n\n  final safeResult = schema.safeParse({\n    'name': null,\n    'age': 0, // 0 might be invalid if min is greater than 0\n  });\n  if (!safeResult['success']) {\n    safeResult['errors'].forEach((error) =\u003e print(\"Safe Error: $error\"));\n  } else {\n    print(\"Safe Parsed Value: ${safeResult['data']}\");\n  }\n}\n```\n\n\u003cbr\u003e\n\n### Error Handling with ZardError 😵‍💫\n\nWhen a validation fails, Zard provides detailed error information via the `ZardError` class. Each error object contains:\n\n- **message**: A descriptive message about what went wrong.\n- **type**: The type of error (e.g., `min_error`, `max_error`, `type_error`).\n- **value**: The unexpected value that failed validation.\n\nZard supports two methods for validation:\n\n1. **`parse()`**: Throws an exception if any validation fails.\n2. **`safeParse()`**: Returns an object with a `success` flag and a list of errors without throwing exceptions.\n\n\u003cbr\u003e\n\n### New Methods \u0026 Functionality\n\nZard now supports additional methods to handle optional and nullable values as well as partial map validations using `.omit()` and `.pick()`.\n\n- **`.nullable()`**: Accepts `null` values and bypasses further validations.\n- **`.optional()`**: Marks the schema as optional; if a value is missing, validations are skipped.\n- **`.omit()`**: Excludes specific keys from being validated in a Map schema.\n- **`.pick()`**: Validates only a selected subset of keys in a Map schema.\n\n\u003cbr\u003e\n\n### Similarity to Zod\n\nZard was inspired by Zod, a powerful schema validation library for JavaScript. Just like Zod, Zard provides an easy-to-use API for defining and transforming schemas. The main difference is that Zard is built specifically for Dart and Flutter, harnessing the power of Dart's language features.\n\n\u003cbr\u003e\n\n## Contribution\n\nContributions are welcome! Feel free to open issues and pull requests on the [GitHub repository](https://github.com/evandersondev/zard).\n\n\u003cbr\u003e\n\n## License\n\nThis project is licensed under the MIT License. See the [LICENSE](LICENSE) file for more details.\n\n---\n\nMade with ❤️ for Dart/Flutter developers! 🎯✨\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevandersondev%2Fzard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fevandersondev%2Fzard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fevandersondev%2Fzard/lists"}