parseOrThrow throws TypeError instead of validation error for `array<option<_>>` inside object
Hi, thanks for this library β it's been great to work with! Iβve run into a bug when validating an object that includes a field of type array<option<string>>.
When the JSON input does not include the expected field (e.g. missing foobar), instead of getting a validation error, parseOrThrow crashes with:
TypeError: Cannot read properties of undefined (reading 'length')
at eval (eval at internalCompile (.../node_modules/sury/src/Sury.res.js:927:10), <anonymous>:3:83)
at Object.parseOrThrow (.../node_modules/sury/src/Sury.res.js:1027:32)
π₯ Expected A SuryError indicating that the field foobar is missing or invalid
π Reproduction
type t = {foobar: array<option<string>>}
let testSchema = S.schema(s => {
foobar: s.matches(S.array(S.option(S.string))),
})
let json = (%raw(`{"weird": true}`): Js.Json.t)
Js.log(json->S.parseOrThrow(testSchema))
β Other working cases These similar cases return expected SuryError eg. Failed parsing at ["foobar"]: Expected string[], received undefined
Object with array of string
type t = {foobar: arrayObject with optional string
type t = {foobar: optionTop-level array of option
type t = arrayIt looks like the issue specifically arises when:
- a field is an array of optional values, and
- that field is defined inside an object schema, and
- the input is missing or doesn't match the expected structure.
π Additional context
I realise that an array<option<_>> is not something that maps to JSON β but I'm using it for GraphQL types, where a field might be [String] (nullable elements). I'm using your sury-ppx to decorate those types with @s.nullable option
Hereβs another similar schema that also throws the same error:
type t = {foobar: array<Js.Nullable.t<string>>}
let testSchema = S.schema(s => {
foobar: s.matches(S.array(S.nullable(S.string))),
})
let json = (%raw(`{"weird": true}`): Js.Json.t)
Js.log(json->S.parseOrThrow(testSchema))
Let me know if you'd like a minimal reproduction repo or failing test β happy to help further.
Thanks again! π
Thanks for reporting π This is a critical issue my tests missed. I'm going to release a fix as 10.0.2 in a few minutes.
This is fantastic. I appreciate it β thank you!