question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Errors in `onError` loose all ajv properties when a custom `validator` property is specified.

See original GitHub issue

Prerequisites

Description

When a custom validator is passed through the validate property, all properties except the stack property on onError errors are wiped.

Steps to Reproduce

See https://codesandbox.io/s/react-example-22s01 Click submit to see proper errors. Click “Enable custom validation” to add a no-op custom validator, and submit again. Observe errors now are missing most of their properties.

Expected behavior

Existing AJV errors should mantain their properties. Custom validator errors should at the very least specify the property property so consumers of onError know what field is having the error.

Actual behavior

All errors, both custom and ajv, only have a stack property.

Error with custom validation off: (all properties specified as expected)

[
  {
    "name": "maxLength",
    "property": ".test",
    "message": "should NOT be longer than 3 characters",
    "params": {
      "limit": 3
    },
    "stack": ".test should NOT be longer than 3 characters",
    "schemaPath": "#/properties/test/maxLength"
  }
]

Error with custom validation on: (No properties aside from stack specified for both schema and custom errors)

[
  {
    "stack": "test: should NOT be longer than 3 characters"
  },
  {
    "stack": "test: This is a custom error"
  }
]

Version

1.8.1

Cause:

The error properties are generated by transformAjvErrors here. If custom validation is off, this is the error object returned here.

However, if a custom validator is used, the error list is entirely regenerated by toErrorList here. This function only populates the stack property.

Fix

Fixing this might be difficult if we want to completely regenerate the list to allow the validate handler to delete existing errors.
However, at the very least we should populate the property key in toErrorList so that the consumer knows where the errors occurred. This would be enough to fix our particular use case.

Edit: validate does not receive the existing errors, but a new schema object. This means the user cannot delete existing errors and we can simply compute the toErrorList of the validator errors and concat it.

We can accomplish this by replacing the code here

  const errorHandler = customValidate(formData, createErrorHandler(formData));
  const userErrorSchema = unwrapErrorHandler(errorHandler);
  const newErrorSchema = mergeObjects(errorSchema, userErrorSchema, true);
  // XXX: The errors list produced is not fully compliant with the format
  // exposed by the jsonschema lib, which contains full field paths and other
  // properties.
  const newErrors = toErrorList(newErrorSchema);

with

  const errorHandler = customValidate(formData, createErrorHandler(formData));
  const userErrorSchema = unwrapErrorHandler(errorHandler);
  const newErrorSchema = mergeObjects(errorSchema, userErrorSchema, true);
  // Concat the new errors to the existing error list
  const newErrors = [].concat(errors, toErrorList(userErrorSchema));

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
heath-freenomecommented, Aug 28, 2022
0reactions
penxcommented, Jul 24, 2022

Not stale. Open PR at #2002

Read more comments on GitHub >

github_iconTop Results From Across the Web

ajv-errors - Ajv JSON schema validator
Custom error messages in JSON-Schema for Ajv validator ... be used if any error appears that is not specified by keywords/properties/items using _...
Read more >
Keyword messages for 'required' and 'additionalProperties' not ...
According to the docs, 'for keywords "required" and "dependencies" it is possible to specify different messages for different properties'.
Read more >
AJV return only one error although there is multiple
Save this question. Show activity on this post. I'm trying to use AJV with the below code, when I validate an object with...
Read more >
Validation Error - Ng Alain
A set of error messages may be created during JSON Schema validation, every error has a ... ui.errors (for a specific property) to...
Read more >
onion-form - npm Package Health Analysis | Snyk
Snyk scans all the packages in your projects for vulnerabilities and provides ... Field, Submit } from 'onion-form'; <Form name="signIn" onError={({ errors }) ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found