kristianmandrup / schema-to-yup

Schema to Yup validation
Other
280 stars 51 forks source link

Nullable value not evaulated properly #130

Closed d-lach closed 1 year ago

d-lach commented 1 year ago

I receive an error message: image

however the field is marked as nullable in the schema:

{
  type: 'object',
  nullable: true,
  required: false,
  properties: {
    (...)
  }
}
kristianmandrup commented 1 year ago

This is the constraint logic for applying nullable (in mixed.js) where value is the actual JSON schema constraint object (ie in terms of key/value) where key is the property name.

  nullable() {
    const nullable = this.value.nullable === true;
    this.base = (nullable && this.base.nullable()) || this.base;
    return this;
  }

nullable methid should be called since it is part of

  get mixedEnabled() {
    return (
      this.mixedConfig.enabled || [
        "oneOf",
        "notOneOf",
        "when",
        "nullable",
        "isType",
        "label",
        "const",
        "refValueFor",
      ]
    );
  }
  get enabled() {
    return [...this.mixedEnabled, ...this.$typeEnabled];
  }

  convertEnabled() {
    this.enabled.map((name) => {
      const convertFn = this.convertFnFor(name);
      if (convertFn) {
        convertFn(this);
      }
    });
  }

You can debug it by applying detailed logging as described in the docs

kristianmandrup commented 1 year ago

The branch nullable now has tests to verify that nullable constraint should work as expected

kristianmandrup commented 1 year ago

Please see PR - https://github.com/kristianmandrup/schema-to-yup/pull/143