0

Currently I have a t.Number field.

When I call t.validate() on it, it gives me the following result.errors:

{
    "actual": "James King", 
    "message": "Invalid value \"James King\" supplied to /user_id: Number",
    "path": ["user_id"]
}

Is it possible to modify the output of the t.Number validation to have:

{
    "actual": "James King", 
    "message": "Oops! The value is invalid", 
    "path": ["user_id"]
}

Also, am I getting that results.errors because I am in NODE_ENV "development"? If so, what results.errors will I get if I am in "production"?

granch
  • 225
  • 3
  • 12
edster
  • 116
  • 2
  • 10

1 Answers1

0

I figured it out in the end.

As per: https://github.com/gcanti/tcomb-validation#customise-error-messages

Attach a getValidationErrorMessage() function to the 'type'. i.e.

Number.getValidationErrorMessage = (value, path, context) => {
  return '"'+value+'" is invalid. It must be a number.';
}

Then made the call to tcomb-validate's validate():

const result = t.validate('s', Number);

It gave the result with the customized error message:

result Struct {
  errors: 
   [ Struct {
       message: '"s" is invalid. It must be a number.',
       actual: 's',
       expected: [Object],
       path: [] } ],
  value: 's' }
edster
  • 116
  • 2
  • 10