Skip to content

3.9.0

Choose a tag to compare

@plutext plutext released this 25 Oct 22:42
· 12 commits to master since this release

An integer attribute can now be read strictly, so that only an optional sign and digits are accepted. This is a per-call unmarshaller option, off by default:

context.createUnmarshaller({
  strictIntegers: true,
  onInvalidValue: function (text, propertyInfo, classInfo, node, error) { /* ... */ }
})

What it does. The integer parse reads text with Number(). So "", " 240 ", 240., 10206.0, 0x10 and 1e3 all read as integers, and none of them reach 3.7.0's onInvalidValue. With strictIntegers, an attribute of Integer or any of its twelve restrictions (Long, Int, UnsignedLong, ...) accepts only /^[+-]?[0-9]+$/.

  • Every other text is refused. It goes to onInvalidValue exactly as written or, if that is unset, fails with a located error: w:spacing/@w:before (line 3): Argument [ 240 ] must be an integer literal: an optional + or - and the digits 0 to 9, with nothing around them.
  • A text that the lenient parse already refuses (1.5, abc) fails with the same error as before.
  • No whitespace is trimmed. This is deliberately stricter than XSD, which collapses whitespace in xs:integer, because Word 365 refuses a document holding " 240 ".

Reading as before. propertyInfo.typeInfo.parse(text) is still the lenient parse. A callback can call it to keep the runtime's reading for some vocabularies: it returns the value, or throws the error, as without the option. The typings add TypeInfo.parse? for this, and UnmarshallerOptions.strictIntegers?.

Unchanged: element content, simple content, list items, enumerations, Decimal/Double, range checks (there are none, on or off) and marshalling. The Context constructor does not read the option.

Why. docx4j CR-065's shared Word 365 probe found the following:

  • Word refuses the whole document for whitespace around an integer, for hex and for exponents.
  • It reads "" as 0 and a decimal by truncation.

@docx4j/generated-objects-ts reads Word's forms through onInvalidValue (its CR-015). Until now, the forms Number() accepts never reached it.

Cost. Unset, unmarshalling is within noise of 3.8.0. With the option on, the check costs about 40 ns per integer attribute.

What a consumer must do. Nothing.

Change request: docs/change-requests/jsonix-CR-012-strict-integer-attributes.md