kristianmandrup / schema-to-yup

Schema to Yup validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

JSON schemas with an array in `type` result in an error

itsjavi opened this issue · comments

I found that when the JSON schema has a type array, the library fails with TypeError: Found non-callable @@iterator.

Example property:

fooBar: { type: ['string', 'null'] }

Here I specify that the possible values are a string or null.
The same happens if you specify an array with other types.

That's valid JSON schema as you can see in the last example here: https://json-schema.org/understanding-json-schema/reference/type.html?highlight=null

Also, in case the lib cannot understand the JSON Schema it would be nice to have a more specific error

Indeed. Check out  toMultiType() function in property-value-resolver. It currently bound to the wrong method (copy pasta 🍝 error).

Either see if you can clone, fix and make and PR for it or I will see if I have time upcoming weekend.

There is also the option to provide you own resolver via config method as you can see in the code.

I've fixed it recently so it is now bound to the correct method.

Hi,
I'm still experiencing this issue on version 1.12.10
Schema:

{
    type: "object",
    properties: {
       targetSelector: {
        type: ["string"],
      },
    }
}

stacktrace:

Uncaught TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
    at at.error (index.js:1:37389)
    at at.toEntry (index.js:1:37525)
    at Object.ot [as createYupSchemaEntry] (index.js:1:37717)
    at ct.createYupSchemaEntry (index.js:1:40701)
    at ct.propToYupSchemaEntry (index.js:1:40533)
    at ct.reducePropToShape (index.js:1:40375)
    at index.js:1:40290
    at Array.reduce (<anonymous>)
    at ct.objPropsToShape (index.js:1:40290)
    at ct.propsToShape (index.js:1:40025)
error @ index.js:1
toEntry @ index.js:1
ot @ index.js:1
createYupSchemaEntry @ index.js:1
propToYupSchemaEntry @ index.js:1
reducePropToShape @ index.js:1
(anonymous) @ index.js:1
objPropsToShape @ index.js:1
propsToShape @ index.js:1
init @ index.js:1
ct @ index.js:1
ht @ index.js:1
../shared/src/api-client/types.ts @ types.ts:192
__webpack_require__ @ bootstrap:24
fn @ hot module replacement:62
../shared/src/api-client/index.ts @ error.ts:23
__webpack_require__ @ bootstrap:24
fn @ hot module replacement:62
../shared/src/index.ts @ useWarehouses.ts:20
__webpack_require__ @ bootstrap:24
fn @ hot module replacement:62
./src/main.tsx @ useDebounce.ts:17
__webpack_require__ @ bootstrap:24
(anonymous) @ startup:6
__webpack_require__.O @ chunk loaded:23
(anonymous) @ startup:7
(anonymous) @ startup:7```

The error regarding an error for types has unfortunately never been resolved. There is support for an array type which should be activated when it encounters a property of the form:

type: 'array'

So for the type: ['string'] case it needs to be normalized to this form.

I would advise looking at the MultiPropertyValueResolver class and unit test it for your specific cases.

Note that it supports a config.toMultiType function where you can implement your own custom solution to suit your case.

Why don't you all work together on it and submit a PR with fixes to these issues. I was always hoping for the community to help complete the implementation to handle the edge cases.