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

trouble with native compatibility with ajv-i18n #1195

Closed
RUVATA opened this issue Feb 25, 2019 · 7 comments
Closed

trouble with native compatibility with ajv-i18n #1195

RUVATA opened this issue Feb 25, 2019 · 7 comments
Labels

Comments

@RUVATA
Copy link

RUVATA commented Feb 25, 2019

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

@epicfaace
Copy link
Member

I think this issue should be solved once rjsf supports localization as an option, see #846

@RUVATA
Copy link
Author

RUVATA commented Feb 25, 2019

I think this issue should be solved once rjsf supports localization as an option, see #846

Great thank's for reference.

May be yes, accorded functional may be solve this trouble. Or may be not - because trouble in structure of base error object.
May be - my declaration of trouble will help to ensure compatibility with ajv-i18n

@RUVATA
Copy link
Author

RUVATA commented Feb 25, 2019

I am see the pull request, and i'am dont shure that added functionality are provides compatibility with ajv-i18n native provided functions

at first - localize fuctions need incoming array of ajv validation errors
but in target solution - each error message text will be resolved separatly call of function that placed in 'localization' poperty.
those, user will write some wrapper or other abstraction on base exported (from ajv-i18n) functions

or, i not uderstand 'convertMessagesText' magic :)

In my solution - compatibility is achieved by modify only one property in basic error object (it's not clear why native ajv error is modifing inside of rjsf) and localize will be used 'as is' in existed transformErrors gear (not neead to add new rjsf property for this)

@epicfaace
Copy link
Member

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

@RUVATA
Copy link
Author

RUVATA commented Feb 26, 2019

Ok, thanks for your explanation.

remains to whait for the target pull request (#846) will be aproved and the upcoming next master major version of rjsf released.

I understand correctly ?

@epicfaace
Copy link
Member

@RUVATA Exactly!

@stale
Copy link

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.

@stale stale bot added the wontfix label Apr 15, 2022
@stale stale bot closed this as completed May 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
2 participants