fix(435): reject non-integer relative-date N (1e2/7.0) — strict digit-string validation
- validation.ts: ruleError now uses /^\d+$/ regex instead of Number.isInteger - dateMacro.ts: isValidN now uses /^\d+$/ regex instead of Number.isInteger - Prevents accepting scientific/decimal notation (e.g. '1e2', '7.0') that break compile↔parse contract - Added tests for both invalid cases returning null/error Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -29,6 +29,12 @@ describe('compileRelative', () => {
|
||||
it('returns null when N is empty', () => {
|
||||
expect(compileRelative(r({ value: '' }))).toBeNull();
|
||||
});
|
||||
it('returns null when N is decimal notation', () => {
|
||||
expect(compileRelative(r({ value: '7.0' }))).toBeNull();
|
||||
});
|
||||
it('returns null when N is scientific notation', () => {
|
||||
expect(compileRelative(r({ value: '1e2' }))).toBeNull();
|
||||
});
|
||||
it('returns null when unit is undefined', () => {
|
||||
expect(compileRelative(r({ unit: undefined }))).toBeNull();
|
||||
});
|
||||
|
||||
@@ -11,8 +11,8 @@ const UNITS: DateUnit[] = ['day', 'week', 'month', 'year'];
|
||||
export const RELATIVE_SYNTHETIC = /^(released|added)_(inthelast|notinthelast)$/;
|
||||
|
||||
function isValidN(value: string): boolean {
|
||||
const n = Number(value);
|
||||
return Number.isInteger(n) && n > 0;
|
||||
const t = value.trim();
|
||||
return /^\d+$/.test(t) && Number(t) > 0;
|
||||
}
|
||||
|
||||
export function compileRelative(rule: Rule): string | null {
|
||||
|
||||
@@ -21,6 +21,12 @@ describe('ruleError', () => {
|
||||
expect(ruleError(r({ field: 'release_date', operator: 'inLast', value: '0', unit: 'day' }))).toBeTruthy();
|
||||
expect(ruleError(r({ field: 'release_date', operator: 'inLast', value: '7', unit: 'day' }))).toBeNull();
|
||||
});
|
||||
it('flags relative-date rule with decimal notation N', () => {
|
||||
expect(ruleError(r({ field: 'release_date', operator: 'inLast', value: '7.0', unit: 'day' }))).toBeTruthy();
|
||||
});
|
||||
it('flags relative-date rule with scientific notation N', () => {
|
||||
expect(ruleError(r({ field: 'release_date', operator: 'inLast', value: '1e2', unit: 'day' }))).toBeTruthy();
|
||||
});
|
||||
});
|
||||
|
||||
describe('groupHasErrors', () => {
|
||||
|
||||
@@ -9,8 +9,8 @@ export function ruleError(rule: Rule): string | null {
|
||||
return null;
|
||||
}
|
||||
if (RELATIVE_DATE.has(rule.operator)) {
|
||||
const n = Number(rule.value);
|
||||
if (!Number.isInteger(n) || n <= 0) return 'Enter a whole number greater than zero.';
|
||||
const t = rule.value.trim();
|
||||
if (!/^\d+$/.test(t) || Number(t) <= 0) return 'Enter a whole number greater than zero.';
|
||||
return null;
|
||||
}
|
||||
if (rule.value.trim() === '') return 'A value is required.';
|
||||
|
||||
Reference in New Issue
Block a user