zod
zod copied to clipboard
feat: z.string.creditCard() - support Credit Card Validation
issue: https://github.com/colinhacks/zod/issues/4039
Summary by CodeRabbit
Summary by CodeRabbit
- New Features
- Added robust credit card validation to string input checks, ensuring proper formatting and verification with clear error feedback.
- Tests
- Integrated comprehensive tests verifying various valid and invalid credit card formats, ensuring reliable validation across multiple card types.
- Documentation
- Updated README files to include the new
creditCard()method in thez.string()schema for credit card validation.
- Updated README files to include the new
Walkthrough
The changes integrate credit card validation into the string validation library. Updates extend the StringValidation type in both the deno and src modules by adding a new literal "creditCard". New methods and a validation function (using regex and the Luhn algorithm) are added to the string type to enable credit card checks. Corresponding tests have been introduced to verify both valid and invalid credit card numbers, and the validation chain now supports custom error messages.
Changes
| File(s) | Change Summary |
|---|---|
deno/.../ZodError.ts, src/.../ZodError.ts |
Updated StringValidation type to include "creditCard", modifying object syntax (e.g., removing semicolon) to support the new validation. |
deno/.../__tests__/string.test.ts, src/.../__tests__/string.test.ts |
Added tests for credit card validation, asserting that valid numbers pass and invalid ones produce an error ("Invalid credit card number"). |
deno/.../__tests__/validations.test.ts, src/.../__tests__/validations.test.ts |
Enhanced instantiation tests by appending creditCard() calls to z.string(), including variants with a custom error message. |
deno/.../types.ts, src/.../types.ts |
Introduced a new check in ZodStringCheck for credit cards, added the isValidCreditCard function (using regex and the Luhn algorithm), and defined new methods creditCard() and the getter isCreditCard. |
README.md, README_KO.md, README_ZH.md, deno/lib/README.md |
Added documentation for the new creditCard() method in the z.string() schema, explaining its purpose for credit card validation. |
Sequence Diagram(s)
sequenceDiagram
participant User
participant Schema as z.string()
participant Parser as _parse()
participant Validator as isValidCreditCard()
participant Tester as safeParse()
User->>Schema: Chain .creditCard() / .creditCard({ message })
Tester->>Schema: Validate input
Schema->>Parser: Execute _parse(input)
Parser->>Validator: Check credit card format
Validator-->>Parser: Return true/false
Parser-->>Tester: Return success/error details
Poem
I'm a hopping rabbit, quick and keen,
Adding credit card checks to keep our code clean.
Valid numbers march, while fakes take flight,
With regex and Luhn, everything's just right.
My bunny heart leaps with glee each day—
Thump-thump, our validations lead the way!
🪧 Tips
Chat
There are 3 ways to chat with CodeRabbit:
- Review comments: Directly reply to a review comment made by CodeRabbit. Example:
I pushed a fix in commit <commit_id>, please review it.Generate unit testing code for this file.Open a follow-up GitHub issue for this discussion.
- Files and specific lines of code (under the "Files changed" tab): Tag
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai generate unit testing code for this file.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.@coderabbitai read src/utils.ts and generate unit testing code.@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.@coderabbitai help me debug CodeRabbit configuration file.
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.
CodeRabbit Commands (Invoked using PR comments)
@coderabbitai pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile to the root of your repository. - Please see the configuration documentation for more information.
- If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation:
# yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json
Documentation and Community
- Visit our Documentation for detailed information on how to use CodeRabbit.
- Join our Discord Community to get help, request features, and share feedback.
- Follow us on X/Twitter for updates and announcements.
Deploy Preview for guileless-rolypoly-866f8a ready!
Built without sensitive environment variables
| Name | Link |
|---|---|
| Latest commit | b457e557d4a27ea6e38d7707242952fec930e7c1 |
| Latest deploy log | https://app.netlify.com/sites/guileless-rolypoly-866f8a/deploys/67dc1648e206b30008c3bad7 |
| Deploy Preview | https://deploy-preview-4037--guileless-rolypoly-866f8a.netlify.app |
| Preview on mobile | Toggle QR Code...Use your smartphone camera to open QR code link. |
To edit notification comments on pull requests, go to your Netlify site configuration.
@RobinTail Yes, there is motivation to implement it in v4. Should I implement it using ZodJWT as a reference? 👀 https://github.com/ysknsid25/zod/blob/ad2fc5ee221bc5fbfad1b7695c71a8a2c9c57314/packages/zod/src/v4/core/schemas.ts#L879-L894
@RobinTail It should be fixed so that it can be used with v4. Please check 🙏
would love to see that implemented! how can i help?