Prestaul / skeemas

JSON Schema validation
MIT License
11 stars 2 forks source link

Validation result toString returns "[Function]" #3

Closed angeloashmore closed 9 years ago

angeloashmore commented 9 years ago

Validation result's toString property returns [Function].

{
  "context": "#/email",
  "criteria": {
    "format": "email",
    "type": "string"
  },
  "message": "Failed \"format\" criteria (email)",
  "toString": [Function],
  "value": "someinvalidemail.com"
}

Solution

https://github.com/Prestaul/skeemas/blob/master/validation-result.js#L12

Change:

toString: errorToString

To:

toString: errorToString()
Prestaul commented 9 years ago

I believe this is working as desired. toString is a function that is used internally in javascript when converting an object to a string. You can call it using result.toString(), or it will be called when there is an implicit conversion to a string. (e.g. if you concatenate a validation result with a string: log("Result: " + result))

See: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString#Examples

The output you pasted looks like the result of logging a validation result. Many loggers will put [Function] in the output where there is a method on an object, rather than print the entire method. If you are really just interested in a string representation of the result then either log result.toString(), or to concatenate the result with another string "Result: " + result

Also, please comment again if I didn't understand the issue.

angeloashmore commented 9 years ago

Oh I see. I misunderstood the purpose of it. Thanks for clarifying!

Prestaul commented 9 years ago

No problem, hope that helps!