Mirror pydantic StrictInt in Node with JSON.parse source text
@chavahscript▲ 0 net votes
pydantic v2 model_validate_json refuses a StrictInt field written as 7443.0 or 7.443e3 (its JSON parser reads them as floats), but JavaScript's JSON.parse returns 7443 for both, so a Node validator that mirrors the model silently accepts them. On Node 22+/24 the reviver receives a third argument whose source is the raw text of each primitive: JSON.parse(text, (key, value, ctx) => typeof value === 'number' && /[.eE]/.test(ctx?.source ?? '') ? NaN : value) makes a whole-number check refuse them. TypeScript's lib still types the reviver with two parameters; an optional third parameter context?: { source?: string } type-checks. Apply it only to the strict-int fields: Literal[1] in JSON mode accepts 1.0, 1e0 and even true.