@@ -68,6 +68,34 @@ const result = evaluator.validate(instance,
6868console .log (result); // true or false
6969```
7070
71+ ### Parsing large integers
72+
73+ JavaScript's
74+ [ ` JSON.parse ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse )
75+ silently truncates integers that exceed
76+ [ ` Number.MAX_SAFE_INTEGER ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/MAX_SAFE_INTEGER )
77+ to IEEE 754 double-precision floats. If your schemas or instances contain large
78+ integers, pass ` Blaze.reviver ` to ` JSON.parse ` to preserve them as
79+ [ ` BigInt ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt )
80+ values. This relies on the
81+ [ ` JSON.parse ` source text
82+ access] ( https://github.com/tc39/proposal-json-parse-with-source ) proposal
83+ (Stage 4, shipped in all major engines):
84+
85+ ``` javascript
86+ const template =
87+ JSON .parse (readFileSync (" template.json" , " utf-8" ), Blaze .reviver );
88+ const evaluator = new Blaze (template);
89+
90+ const instance =
91+ JSON .parse (readFileSync (" instance.json" , " utf-8" ), Blaze .reviver );
92+ console .log (evaluator .validate (instance));
93+ ```
94+
95+ The evaluator handles ` BigInt ` values natively in all numeric instructions.
96+ Without a reviver, large integers are silently truncated and validation may
97+ produce incorrect results for affected values.
98+
7199## Why compile separately?
72100
73101Unlike validators that compile and evaluate in a single step, Blaze separates
@@ -95,24 +123,18 @@ against your data, with no knowledge of JSON Schema itself.
95123
96124## Limitations
97125
98- ** No high-precision decimal support.** Compiled templates preserve
99- arbitrary-precision numbers exactly as they appear in your schemas. However,
100- JavaScript's
101- [ ` JSON.parse ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse )
102- silently truncates all numbers to IEEE 754 double-precision floats before the
103- evaluator ever sees them. While JavaScript has
104- [ ` BigInt ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt ) ,
105- [ ` JSON.parse ` ] ( https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse )
106- does not use it. This is a language-level limitation that affects any numeric
107- keyword that depends on exact arithmetic. For example:
126+ ** No arbitrary-precision real number support.** Large integers can be preserved
127+ as ` BigInt ` using a reviver (see above), but JavaScript has no equivalent type
128+ for arbitrary-precision real numbers. ` JSON.parse ` truncates values like
129+ ` 0.1000000000000000000000000000000001 ` to IEEE 754 doubles, and there is no
130+ reviver-based workaround:
108131
109- ``` sh
110- $ node --eval " console.log(JSON.parse('9007199254740993 '))"
111- 9007199254740992 # Note the result is off by 1
132+ ```
133+ $ node --eval "console.log(JSON.parse('0.1000000000000000000000000000000001 '))"
134+ 0. 1
112135```
113136
114- The TC39 [ ` JSON.parse ` source text
115- access] ( https://github.com/tc39/proposal-json-parse-with-source ) proposal
116- (Stage 4, shipped in all major engines) provides a path forward by exposing the
117- raw source text to a reviver function. We plan to take advantage of this in a
118- future release.
137+ Numeric keywords like ` multipleOf ` that depend on exact decimal arithmetic may
138+ produce incorrect results for real values that lose precision during parsing.
139+ The TC39 [ Decimal proposal] ( https://github.com/tc39/proposal-decimal ) (Stage 2)
140+ aims to address this in the future.
0 commit comments