jquense / yup

Dead simple Object schema validation
MIT License
22.93k stars 934 forks source link

When using Yup version 1.3.3, there is a change in behavior compared to version 0.32.11 when validating JSON strings with array and object schemas. #2167

Closed Tej81r closed 9 months ago

Tej81r commented 10 months ago

Code that worked in Yup 0.32.11:

const schema1 = yup.array();
const argument = '[{"param1": "val1", "param2": "val2"}]';
params = await schema1.validate(argument);

Error with Yup 1.3.3:

this must be a array type, but the final value was: `"[{\"param1\":\"val1\",\"param2\":\"val2\"}]"`

Expected Behavior:

The validation should work as it did in version 0.32.11, allowing the automatic parsing of JSON strings when validating against array and object schemas.

Similar Issue with Object Schema:

In addition to the reported issue with array schemas, the same behavior is observed when using object schemas. Here's an example:

const schema2 = yup.object();
const argumentObject = '{"param1": "val1", "param2": "val2"}';
paramsObject = await schema2.validate(argumentObject);

Error with Yup 1.3.3:

this must be an object type, but the final value was: `"{"param1": "val1", "param2": "val2"}"`

Expected Behavior:

The validation for object schemas should also work as expected, allowing automatic parsing of JSON strings.

jquense commented 9 months ago

correct there is a change in behavior json is not automatically parsed anymore, you can use json() method now . Please see the changelogs and guides for migrating