Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle validation messages returned by Sails JS?

Hope you are doing great!!

I am working on SailsJS web-app and using actions structure for controllers.

This is my user/login action:

  module.exports = {
    friendlyName: 'Login',

    description: 'Login user.',

    inputs: {
      email: {
        description: 'user email.',
        type: 'string',
        required: true
      },
      password: {
        description: 'user password.',
        type: 'string',
        required: true
      }
    },

    exits: {
      success: {
        statusCode: 200,
        responseType: 'view',
      },       
    },

    fn: async function (inputs, exits) {
      // All done.
      return 'hello world';
    }
  };

This is the login form html code:

    <div class="row">
      <div class="valign-wrapper">
          <div class="col s6 z-depth-4 card-panel">
            <form class="col s12 login-form" method="POST" action="/login">
              <input type="hidden" name="_csrf" value="<%= _csrf %>" />
              <div class="card-content">
                <h5 class="card-title center-align">Login</h5>
                <div class="row margin">
                  <div class="input-field col s12">
                    <input id="email" type="email" class="validate">
                    <label for="email">Email</label>
                    <div class="invalid-feedback" v-if="formErrors.emailAddress">Please provide a valid email address.</div>
                  </div>
                </div>
                <div class="row margin">
                  <div class="input-field col s12 right">
                    <input id="password" type="password" class="validate">
                    <label for="email">Password</label>
                  </div>
                </div>
                <div class="row">
                  <button class="btn waves-effect waves-light right" type="submit" name="action">Login</button>
                </div>            
              </div>
            </form>
          </div>
        </center>
      </div>
    </div>

Whenever, I am submitting blank form I get this following error:

    {
      "code": "E_MISSING_OR_INVALID_PARAMS",
      "problems": [
        "\"email\" is required, but it was not defined.",
        "\"password\" is required, but it was not defined."
      ],
      "message": "The server could not fulfill this request (`POST /login`) due to 2 missing or invalid parameters.  **The following additional tip will not be shown in production**:  Tip: Check your client-side code to make sure that the request data it sends matches the expectations of the corresponding parameters in your server-side route/action.  Also check that your client-side code sends data for every required parameter.  Finally, for programmatically-parseable details about each validation error, `.problems`. "
    }

Now, the issue is that I am unable to find a way to handle this error in prettier way.

I hope one of you can guide me on the right path.

Thank You,

like image 488
BlueSuiter Avatar asked Oct 16 '22 09:10

BlueSuiter


1 Answers

U can transform error in api/responses/badRequest

module.exports = async function badRequest(data) {
  const status = 400;

  if (data.code && data.code === 'E_MISSING_OR_INVALID_PARAMS'){
    return this.res.status(status).json({
      code: 'My code',
      message: 'My message',
      whateva: 'Bla bla'
    });
  }

  return this.res.status(status).json(data);
};

I have a custom project and I need the same thing u requested. I made npm package who take an error object and make it prettier for frontend. Something like this:

module.exports = async function badRequest(data) {

  const formatError = require('myFormatError');
  const {status, response}= formatError(data);

  return this.res.status(status).json(response);

};

It doesn't need to be npm, u can make some function. I hope u were looking for this.

like image 93
Dejan Kubaša Avatar answered Nov 01 '22 10:11

Dejan Kubaša