1

I have error messages that appear in the following format:

{
 "message":"The request is invalid.",
 "modelState":{
    "model.ConfirmPassword":["The password and confirmation password do not match."]
  }
}

{
 "message":"The request is invalid.",
 "modelState":{
    "model.Email":["The Email must be at least 10 characters long."],
    "model.ConfirmPassword":["The password and confirmation password do not match."]
  }
}

I know how to get the modelState but how can I get the first message field in the modelState when that field could be different each time?

  • What I would like in the first instance is to get the message: "The password and confirmation password do not match."
  • And in the second instance the message: "The Email must be at least 10 characters long."
  • Here's a link to getting a list of keys from a JS object: http://stackoverflow.com/questions/3068534/getting-javascript-object-key-list – darnmason Jun 10 '14 at 10:52
  • 2
    What do you mean by "first"? The properties in an object like `modelState` do not have an inherent order in JavaScript. – kmoe Jun 10 '14 at 10:53

4 Answers4

1

You can iterate through all object properties using Object.keys or method hasOwnProperty

for (var k in modelState) {
    if (modelState.hasOwnProperty(k)) {               
        var error_message = modelState[k][0];
        // k => "model.Email"
        // error_message =>  "The Email must be at least 10 characters long."
    }
}
cardeol
  • 2,218
  • 17
  • 25
  • This solution ignores the "first error message" constraint which was given in the question. More so it does not even cancel the loop when proper key is found - it will just keep on moving through all of the (potentially) billion error messages. And it will stop on the random "last" error. Get ready to see different error texts on IE8 vs iPhone4 vs iPad 20. – Ingmars Jun 12 '14 at 14:05
  • The question is about getting the first error message inside any key provided by modelState. Each key could contain more than one message for a specific field regardless the field [key] name. This solution has been probed and confirmed. – cardeol Jun 12 '14 at 16:52
  • If you carefully read the text which comes after the original code block, then you'll find yourself wrong. **I know how to get the modelState but how can I get the first message field in the modelState when that field could be different each time?** – Ingmars Jun 12 '14 at 17:07
  • You can discuss your issues with the Author. The problem for me is about getting "foo" from { "PropertyName": ["foo", "bar", "foobar"] }. It is a simple problem with a simple solution. – cardeol Jun 13 '14 at 15:26
0

Most up-to-date browsers support the Object.keys method

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys

You can use that to get an array from your modelState that'll return ["model.Email", "model.ConfirmPassword", ... ] in each case.

Then just get the first item from that return array, and call it, so something along the lines of:

var keys = modelState.keys;
var firstError = modelState[keys[0]];

(I haven't tested that syntax, but it's something along those lines)

(note that "first item" might be a little arbitrary, I'm not sure it's guaranteed the order in which the keys will be returned, although in practice I expect you'd find it pretty reliable)

PulseLab
  • 1,577
  • 11
  • 15
0

Based on the fact that JS object keys are unordered by nature I'd suggest refactoring your modelState response (you have array as value anyway):

var response = {
    "message":"The request is invalid.",
    "modelState": {
        "errors": ["Bad email", "Bad password"]
    }
}
console.log( response.message, response.modelState.errors[0] );
Ingmars
  • 998
  • 5
  • 10
0

V2: Based on the fact that JS object keys are unordered by nature, you can add a defined error priority order.

var response = {
    "message":"The request is invalid.",
    "modelState":{
        "model.Email":["The Email must be at least 10 characters long."],
        "model.ConfirmPassword":["The password and confirmation password do not match."]
    }
}

var priorities = ["model.Email", "model.ConfirmPassword"];
var selectedError = null,
idx = 0;

do {
    selectedError = response.modelState[ priorities[ idx ] ];
    selectedError = selectedError ? selectedError[0] : null;
    idx++;
} while ( !selectedError && idx < priorities.length );

console.log( selectedError || 'Unknown error'  );

http://jsfiddle.net/A2dA9/

Ingmars
  • 998
  • 5
  • 10