Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditionally requiring fields using oneOf #36

Closed
abbasegbeyemi opened this issue Jul 7, 2021 · 1 comment
Closed

Conditionally requiring fields using oneOf #36

abbasegbeyemi opened this issue Jul 7, 2021 · 1 comment

Comments

@abbasegbeyemi
Copy link

Thank you so much for this library. It is absolutely amazing.

I have a schema where at least one field is required. The JSON schema looks like this as an example.

 type: "object",
      $schema: "http://json-schema.org/draft-07/schema#",
      $id: "testform",
      properties: {
        favouriteColour: {
          default: "",
          type: "string",
        },
        favouriteFruit: {
          default: "",
          type: "string",
        },
      },
      required: ["name", "age"],
      oneOf: [
        {
          required: ["favouriteColour"],
        },
        {
          required: ["favouriteFruit"],
        },
      ],
    };

The rules are not getting applied in Yup. I know the examples show oneOf being used to conditionally type a property, but I was wondering if there was a way to use it like this.

Another question I have is - when I define a schema like below:

 const testSchema = {
      type: "object",
      $schema: "http://json-schema.org/draft-07/schema#",
      $id: "testform",
      properties: {
        likes: {
          type: "array",
          default: [],
          minItems: 2
          maxItems: 4,
        },
      },

When I don't include the likes field in the validation data, a validation error is raised that the array field has to have the minimum elements. I have not included the property name in the array of required elements, so I would expect that the validation should only fail if the element is not an empty list? Can you please explain what I am doing wrong in this case? Thanks.

@ritchieanesco
Copy link
Owner

@abbasegbeyemi thanks for raising this issue. A fix has been made and published (json-schema-yup-transformer@1.6.6-beta.2). Validation only executes when field and field value are present.

Based on your schema these are valid.

    valid = yupschema.isValidSync({});
    expect(valid).toBeTruthy();

    valid = yupschema.isValidSync({likes: undefined});
    expect(valid).toBeTruthy();

   valid = yupschema.isValidSync({ likes: [1,2,3] });
   expect(valid).toBeTruthy();

Based on your schema these are NOT valid.

    valid = yupschema.isValidSync({ likes: [] });
    expect(valid).toBeFalsy();

    valid = yupschema.isValidSync({likes:[ 1 ]});
    expect(valid).toBeFalsy();

    valid = yupschema.isValidSync({likes:[ 1, 2, 3, 4, 5 ]});
    expect(valid).toBeFalsy();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants