Giter Site home page Giter Site logo

Comments (3)

epoberezkin avatar epoberezkin commented on May 18, 2024 3

I think approach 2 is ok, why is it a workaround? "required" keyword validates an object, not a property, so dataPath is correct.

from ajv-errors.

Aleksandras-Novikovas avatar Aleksandras-Novikovas commented on May 18, 2024 3

Yes, "required" keyword validates object but "errorMessage.required" allows to have different messages for separate "missing" properties. As you can see from my example there is no direct way to know which message (in error object array) meant for which property within this object. In my projects I add property name to the start of error message to make error processing simpler. It would be more robust solution to receive missing property name from validator.

Simple use case would be:
user fills-in form and posts data to server;
server validates and returns errors with missing required fields;
client application highlights corresponding input fields and shows corresponding message;
Now there is no easy way for client to determine which exact fields were missing and which error message should be shown for which input field.

from ajv-errors.

YannOntiime avatar YannOntiime commented on May 18, 2024

I have the same need and I have developed the beginning of a function that allows to return an array of objects of type :

[
    
    {
        "field": "email",
        "message": "email must be a string and is required"
    }, 
    {
        "field": the name of the field,
        "message": message error
    },
]
function ajvErrorsToArray(ajvErrors: Array<{}>) {
  let errors: Array<{}> = [];
  ajvErrors.forEach((error: any) => {
    let e: { field: string | null; message: string | null } = {
      field: null,
      message: null,
    };
    if (error.instancePath === "") {
      error.params.errors.forEach((paramsError: any) => {
        if (paramsError.keyword === "required") {
          if (error.message) {
            e.field = paramsError.params.missingProperty;
            e.message = error.message;
          } else {
            e.field = paramsError.params.missingProperty;
            e.message = paramsError.message;
          }
        }
      });
    } else {
      e.field = error.instancePath.substring(1);
      e.message = error.instancePath.substring(1) + " " + error.message;
    }
    errors.push(e);
  });

  return errors;
}

from ajv-errors.

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.