Skip to content

feat: phone validation#2672

Open
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:feat/phone-validation
Open

feat: phone validation#2672
sanny-io wants to merge 5 commits into
zenstackhq:devfrom
sanny-io:feat/phone-validation

Conversation

@sanny-io
Copy link
Copy Markdown
Contributor

@sanny-io sanny-io commented May 18, 2026

Adds a @phone attribute and isPhone function that validates E.164 phone numbers.

Closes #1897

Summary by CodeRabbit

  • New Features

    • Added E.164 phone-number validation for string fields: new field-level attribute and corresponding validation rule, with runtime enforcement added to schema validation.
  • Tests

    • Updated unit, integration, and e2e tests to cover phone validation (valid and invalid cases) and to ensure validation attributes are preserved across schema operations.

Review Change Stack

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 18, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 152f9e96-2d30-4aee-b959-d7cdf19b13ab

📥 Commits

Reviewing files that changed from the base of the PR and between c8d2299 and f5680d2.

📒 Files selected for processing (1)
  • tests/e2e/orm/validation/custom-validation.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/e2e/orm/validation/custom-validation.test.ts

📝 Walkthrough

Walkthrough

Adds E.164 phone validation: a @phone attribute and isPhone() stdlib rule, Zod integration applying e164() and mapping isPhone to that check, plus updated/generated schemas and tests (factory, E2E, and DB pull) to cover the behavior.

Changes

Phone Validation Feature

Layer / File(s) Summary
Phone validation attribute and stdlib function definition
packages/language/res/stdlib.zmodel
@phone attribute and isPhone() validation rule function are added to stdlib for E.164 phone validation targeting string fields.
Zod validator implementation for phone validation
packages/zod/src/utils.ts
addStringValidation recognizes @phone and applies e164() to Zod schemas; evalCall dispatcher maps isPhone() calls to Zod's 'e164' validator.
Test schema and fixture updates
packages/zod/test/schema/schema.zmodel, packages/zod/test/schema/schema.ts, packages/zod/test/factory.test.ts
Test schemas introduce phone: String @phone`` field; validUser fixture includes phone value and generated schema reflects the new field.
Factory tests: validation behavior and type inference
packages/zod/test/factory.test.ts
New @phone validation tests reject invalid and accept valid E.164 phone numbers; type-inference assertions across omit, include, select, and optionality configurations are extended to account for the phone field.
End-to-end validation test coverage
tests/e2e/orm/validation/custom-validation.test.ts, tests/e2e/orm/validation/toplevel.test.ts
Custom validation test adds str6: String? with @@validate using isPhone(); toplevel test adds str7: String? @phone`` and verifies rejection and acceptance of phone formats with expected error messages.
Database pull preservation test
packages/cli/test/db/pull.test.ts
Database introspection test extends schema fixture to verify @phone attribute is preserved during db pull.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

🐰 I hop along the schema trail,
A tiny ear tuned to the dial,
E.164 hops tidy and neat,
Tests make every leap complete,
Phone fields sing, my work's worthwhile.

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'feat: phone validation' clearly and concisely describes the main change, adding a new phone validation feature to the system.
Linked Issues check ✅ Passed The implementation adds @phone attribute for E.164 validation and isPhone function as specified in #1897. The implementation covers validation logic across stdlib, zod integration, tests, and E2E examples.
Out of Scope Changes check ✅ Passed All changes directly support phone validation: stdlib definition, Zod utility mapping, comprehensive tests, and E2E validation examples. No unrelated modifications detected.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

tests/e2e/orm/validation/custom-validation.test.ts

Oops! Something went wrong! :(

ESLint: 9.29.0

ESLint couldn't find an eslint.config.(js|mjs|cjs) file.

From ESLint v9.0.0, the default configuration file is now eslint.config.js.
If you are using a .eslintrc.* file, please follow the migration guide
to update your configuration file to the new format:

https://eslint.org/docs/latest/use/configure/migration-guide

If you still have problems after following the migration guide, please stop by
https://eslint.org/chat/help to chat with the team.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (1)
tests/e2e/orm/validation/custom-validation.test.ts (1)

83-85: ⚡ Quick win

Add a positive isPhone assertion in this test loop.

This block only checks rejection; it doesn’t verify that a valid E.164 string passes custom validation (non-null path).

Proposed test addition
             // violates phone
             await expect(_t({ str6: 'not-a-phone' })).toBeRejectedByValidation(['invalid str6']);
+
+            // satisfies phone
+            await expect(_t({ str6: '+15555555555' })).toResolveTruthy();
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@tests/e2e/orm/validation/custom-validation.test.ts` around lines 83 - 85, Add
a positive assertion that the custom isPhone validation accepts a valid E.164
string: call the helper _t with a valid phone value for the str6 field (e.g. an
E.164 formatted number) and assert it does not reject validation; update the
test loop in custom-validation.test.ts next to the existing rejection check so
it verifies both failure for 'not-a-phone' and success for a valid E.164 input.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@packages/zod/test/factory.test.ts`:
- Around line 264-267: The test "rejects invalid phone number for `@phone` field"
is mutating the wrong key; replace the update of website with phone so the
schema validation actually checks the `@phone` rule (i.e., call
factory.makeModelSchema('User') and safeParse with { ...validUser, phone:
'not-a-phone' } instead of updating website) so that result.success will be
false when phone validation fails.

---

Nitpick comments:
In `@tests/e2e/orm/validation/custom-validation.test.ts`:
- Around line 83-85: Add a positive assertion that the custom isPhone validation
accepts a valid E.164 string: call the helper _t with a valid phone value for
the str6 field (e.g. an E.164 formatted number) and assert it does not reject
validation; update the test loop in custom-validation.test.ts next to the
existing rejection check so it verifies both failure for 'not-a-phone' and
success for a valid E.164 input.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 83dcc673-ae78-4055-a655-7fc0b079f692

📥 Commits

Reviewing files that changed from the base of the PR and between ed01275 and 14aaf27.

📒 Files selected for processing (8)
  • packages/cli/test/db/pull.test.ts
  • packages/language/res/stdlib.zmodel
  • packages/zod/src/utils.ts
  • packages/zod/test/factory.test.ts
  • packages/zod/test/schema/schema.ts
  • packages/zod/test/schema/schema.zmodel
  • tests/e2e/orm/validation/custom-validation.test.ts
  • tests/e2e/orm/validation/toplevel.test.ts

Comment thread packages/zod/test/factory.test.ts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature Request] Built-in Phone Validation

1 participant