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.

trouble with native compatibility with ajv-i18n

See original GitHub issue

Prerequisites

Description

Current realization of errors object incomes in transformErrors (https://react-jsonschema-form.readthedocs.io/en/latest/validation/#custom-error-messages) is incompatible with ajv-i18n (https://github.com/epoberezkin/ajv-i18n)

Since the AJV is used internally, it would be nice to ensure compatibility with AJV native i18n gear, especially considering that such compatibility is very simple to provide by expanding the “erorrs” with one property “keyword”, the value for which already exists in members of incoming errors array members as “name” property.

See my playground sample (https://codesandbox.io/embed/vm3lz56j53?fontsize=14), where basic erros array normalizing by map for full compilibility with ajv-i18n localization function.

Steps to Reproduce

  1. Install and import localization (as sample - directly ru) function for ajv-i18n npm package
  2. declare transformErrors fucntion where try to call localization function on basic (non modified) errors incoming argument function transformErrors(errors) { localize_ru(errors); return i18n_normalized; }
  3. You will take error about missing required ‘keyword’ property in ajv-i18n
  4. Then modify your transformErrors, where you previously normilize errors object with basic map function transformErrors(errors) { const i18n_normalized = errors.map(e => ({ ...e, keyword: e.name })); localize_ru(i18n_normalized); return i18n_normalized; }
  5. errors messages are succsefully localizated with ajv-i18n

Expected behavior

The erros array members (objects) will have ‘keyword’ property (duplicated ‘name’ property value)

PS: May be ‘name’ property will be renamed to ‘keyword’ with concurrent refactoring thats usages in other project sources

Actual behavior

The erros array members (objects) not have ‘keyword’ property, and ajv-i18n localization gear crashes called with the current errors array as argumnet

Version

current major - 1.2.1

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
epicfaacecommented, Feb 25, 2019

No, it seems to work fine. If you add the following in playground/samples/localization.js in that PR:

var localize = require('ajv-i18n');

module.exports = {
  schema: {
    type: "object",
    title: "Contextualized localization",
    required: ["name"],
    properties: {
      name: {
        type: "string",
        title: "Nombre",
      },
      active: {
        type: "boolean",
        title: "Activo",
      },
    },
  },
  formData: {
    active: "wrong",
  },
  uiSchema: {},
  localization: localize.ru
};

Then all the error messages are showing up properly in Russian.

image

I believe this is because the localization function is called directly on the errors returned from ajv, before they are transformed to the format that rjsf returns. See https://github.com/mozilla-services/react-jsonschema-form/pull/846/files#diff-6ca5d88062570937214ee7ea244b05c5R161

0reactions
stale[bot]commented, Apr 15, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Please leave a comment if this is still an issue for you. Thank you.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ajv JSON schema validator - npm
Ajv JSON schema validator. The fastest JSON validator for Node.js and browser. Supports JSON Schema draft-04/06/07/2019-09/2020-12 (draft-04 ...
Read more >
Ajv: Another JSON Schema Validator - Global Data Plane
Ajv : Another JSON Schema Validator. The fastest JSON Schema validator for node.js and browser. Supports v5 proposals.
Read more >
Execution environments - Ajv JSON schema validator
The main consideration for using Ajv server-side is to manage compiled schemas correctly, ensuring that the same schema is not compiled more than...
Read more >
Validation - react-jsonschema-form documentation
NOTE: This syntax works only for the @rjsf/validator-ajv6 validator; ... Finally, the Ajv 8 validator supports the localization of error messages.
Read more >
async options, expose Ajv.ValidationError class · 86d97d4337
ajv - Fork of ajv. ... 1. native generator function wrapped with co ... In case of validation failure Ajv assigns the array...
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