Filter out certain errors

Hey, I am wondering if there is an easy way to filter out certain kinds of errors with json forms or the ajv instance that is passed to json forms? I am currently creating my own instance of ajv and passing it to json forms and tried to do some filtering in that via a compile but it just omitted all the errors instead

 // Wrap ajv.compile to include error filtering
  const originalCompile = ajv.compile.bind(ajv);
  ajv.compile = (schema, _options) => {
    const validate = originalCompile(schema, _options);

    // Create a new validate function with error filtering
    const validateWithFilter = (data) => {
      const valid = validate(data);
      console.log('data', data)
      // Filter errors only if there are errors and assign a new array
      if (!valid && validate.errors) {
        const filteredErrors = validate.errors.filter(
          (error) => error.keyword !== 'required',
        );
        console.log('filteredErrors', filteredErrors)
        validate.errors =
          filteredErrors.length > 0 ? filteredErrors : validate.errors;
      }

      console.log('valid', valid)

      return valid;
    };
    return validateWithFilter;
  };

Hi @emmalcg,

errors is an attribute of validate. By returning the custom validateWithFilter instead of validate, JSON Forms can’t access the errors anymore as this attribute does not exist on validateWithFilter.

To fix this, you should set the errors of validateWithFilter to the filteredErrors. There is no need to overwrite the errors of the original validate.

Note that this use case can also be solved via JSON Forms middleware support without the need of handing in a custom AJV. See here for the documentation.

awesome! thank you so much these options are very helpful