Skip to content

Add IsDigitsOnly validator for varchar ID/number fields - #53

Merged
buzz-code merged 1 commit into
mainfrom
claude/add-is-digits-only-validator
Sep 9, 2026
Merged

buzz-code merged 1 commit into
mainfrom
claude/add-is-digits-only-validator

Conversation

@buzz-code

Copy link
Copy Markdown
Owner

Why

Consumer app react-admin-nestjs (buzz-code/react-admin-nestjs#186) needs format validation on varchar ID/number fields (TZ numbers, a teacher/klass "number") that must stay strings to preserve leading zeros — so they're out of reach of the existing numeric validators in class-validator-he.ts.

What

Adds IsDigitsOnly(maxLength?, validationOptions?) to utils/validation/class-validator-he.ts, following this file's conventions: wraps class-validator's ValidateBy the same way every other export here wraps its underlying decorator, Hebrew message via the existing getErrorMessageFunction. Passes through undefined/null/'' (presence stays @IsNotEmpty's job); optionally enforces a max length for fields that don't already have a separate @MaxLength.

Purely additive — no existing export changed, no consumer action required, so no CONSUMER_CHANGES.md row per this repo's own convention.

Testing

Added unit tests in utils/validation/__tests__/class-validator-he.spec.ts following the existing per-decorator describe block pattern. Verified via the react-admin-nestjs consumer checkout (this repo has no standalone test runner of its own — it's consumed as a submodule): 20/20 tests pass, tsc --noEmit clean.

🤖 Generated with Claude Code

https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT


Generated by Claude Code

Some entity fields (a TZ/ID number, a business "number") must stay
varchar rather than a numeric-typed column, to preserve leading zeros.
That put them out of reach of the existing numeric validators here, so
they had no format validation beyond length/presence.

IsDigitsOnly rejects a non-digit string but passes through
undefined/null/'' (presence stays @isnotempty's job, as with every
other validator in this file), with an optional max length for fields
that don't already have one. Follows the file's conventions: Hebrew
message via getErrorMessageFunction, wraps class-validator's
ValidateBy the same way every other export here wraps its underlying
decorator.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT
Copilot AI lite review requested due to automatic review settings September 9, 2026 12:16
@buzz-code
buzz-code merged commit 642725f into main Sep 9, 2026
1 check passed

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

The new validator’s error message can be misleading when failing due to maxLength, and the added test claims to cover null without actually testing it.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

Adds a new shared validation decorator to enforce “digits-only” semantics on varchar ID/number fields that must remain strings (e.g., leading-zero identifiers), along with Jest coverage in the shared validation test suite.

Changes:

  • Added IsDigitsOnly(maxLength?, validationOptions?) to utils/validation/class-validator-he.ts using the existing Hebrew error-message wrapper.
  • Added a dedicated describe('IsDigitsOnly') block in utils/validation/__tests__/class-validator-he.spec.ts.
File summaries
File Description
utils/validation/class-validator-he.ts Introduces IsDigitsOnly via ValidateBy, with optional max-length enforcement and Hebrew message handling.
utils/validation/tests/class-validator-he.spec.ts Adds unit tests validating pass-through behavior and rejection of non-digit strings, plus max-length enforcement.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +59 to +63
return max === undefined || value.length <= max;
},
},
},
{ ...validationOptions, message: getErrorMessageFunction('$property חייב להכיל ספרות בלבד $value') },
Comment on lines +157 to +168
it('should not return an error message for undefined, null or an empty string', async () => {
class Test {
@IsDigitsOnly() key: string;
}

const test = new Test();
const result = await validate(test);
expect(result).toEqual([]);

test.key = '';
expect(await validate(test)).toEqual([]);
});
buzz-code pushed a commit to buzz-code/react-admin-nestjs that referenced this pull request Sep 9, 2026
Per feedback: this validator belongs alongside the other Hebrew-message
class-validator wrappers in shared/utils/validation/class-validator-he.ts,
not as a one-off local file. Moved it there (buzz-code/nra-server#53,
merged), bumped the server/shared submodule pointer, and switched every
entity that used the local src/utils/validation/numeric-string.util
import over to the shared one. Deleted the local file and its test —
the equivalent test now lives in nra-server's own class-validator-he.spec.ts.

No behavior change: same validator, same call sites, now using the
shared getErrorMessageFunction/translationDict machinery for its error
message instead of a hardcoded string.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT
TziporaStern pushed a commit to TziporaStern/nra-server that referenced this pull request Oct 17, 2026
Follows the IsUniqueCombination / IsUniqueDateRange / MaxCountByUserLimit
pattern: the raw decorator lives in its own file using registerDecorator,
class-validator-he.ts just wraps it with the Hebrew message. Dropped the
verbose block comment and the redundant IsDigitsOnly tests in
class-validator-he.spec.ts (IsUniqueCombination/IsUniqueDateRange aren't
tested there either) in favor of a dedicated is-digits-only.spec.ts.

No behavior change from PR buzz-code#53.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016mY7oyUH46UtvEKekiF7fT
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.

3 participants