Code Monkey home page Code Monkey logo

Comments (3)

gabormagyar avatar gabormagyar commented on May 24, 2024 4

For anyone else finding this in the future here's an up-to-date workaround to this problem.

I'm using ajv as part of a jsonforms.io form, so I don't have easy access to the error objects. Alternatively I would have had to add localize calls to each individual form cell renderer, which isn't very clean.

Here I overwrite the ajv.compile function with a wrapper into which I can inject the localization call.

  const ajv = createAjv({messages: false})
  const compileFunction = ajv.compile
  ajv.compile = Object.assign((...compileProps: Parameters<typeof compileFunction>) => {
    const validateFunction = compileFunction.apply(ajv, compileProps)

    const newValidateFunction = Object.assign((...innerProps: Parameters<typeof validateFunction>) => {
      const validationResult = validateFunction.apply(compileFunction, innerProps)
      // @ts-expect-error `ErrorObject` is incorrect type in ajv-i18n: https://github.com/ajv-validator/ajv-i18n/pull/205
      localize(validateFunction.errors)
      Object.assign(newValidateFunction, validateFunction)
      return validationResult
    }, validateFunction)

    return newValidateFunction
  }, compileFunction)

from ajv-i18n.

epoberezkin avatar epoberezkin commented on May 24, 2024 1

Thank you. It is probably better to do in ajv v7-beta that is just released - this implementation will have to be rewritten probably anyway.

from ajv-i18n.

ggondim avatar ggondim commented on May 24, 2024

Actually I just developed it in my fork: ggondim@aaf1458

Example of a localize file (en/index.js) built with npm run build:

'use strict';

function localize_en(errors) {
  // omitted for legibility  
};

function makeLocalizedErrors(ajv) {
  return {
    get: function() {
      return ajv._errors;
    },
    set: function(errors) {
      ajv._errors = localize_en(errors);
    },
    configurable: true
  };
}

function initialize(ajv) {
  ajv._compile = ajv.compile;
  ajv.compile = schema => {
    const validate = ajv._compile(schema);
    Object.defineProperty(validate, 'errors', makeLocalizedErrors(ajv));
    return validate;
  }
}
initialize.localize = localize_en;
module.exports = initialize;

from ajv-i18n.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.