angular-schema-form icon indicating copy to clipboard operation
angular-schema-form copied to clipboard

Format validators don't display correct error message

Open sergedomk opened this issue 10 years ago • 3 comments

This bit of code:

tv4.addFormat("email", function (data, schema) {
  if (typeof data === "string" && /^\S+@\S+$/.test(data)) {
    return null;
  }
  return "A valid email address is required";
});

When applied like this:

email_address: {
  title: "Email Address",
  type: "string",
  maxLength: 256,
  format: "email"
}

Should result in the error:

A valid email address is required

Instead you get:

Format validation failed

From what I can tell the issue appears to be that the only portion of the errors generated by tv4 that are passed on are the error codes.

In src/directives/schema-validate.js:

            ngModel.$setValidity('tv4-' + result.error.code, false);

For format validation using the above format rule, result.error.code would be 500 and result.error.message would be "Format validation failed (A valid email address is required)".

In schema-form, only the tv4 error code is used to select an error message which, of course, has no way of including the format error message returned by tv4.

src/services/errors.js

    500: 'Format validation failed',

sergedomk avatar Dec 16 '15 14:12 sergedomk

I am not sure about your actual question, but isn't a more mainstream thing to do it to use the $validators or do it in the schema instead?

nicklasb avatar Jan 23 '16 23:01 nicklasb

I wasn't posting a question.

The more mainstream approach might be to use the $validators, but then you aren't actually using JSON Schema for validating. One of the nice things about JSON Schema is the validation.

"format" is part of the JSON Schema specification: http://json-schema.org/latest/json-schema-validation.html#anchor104. tv4 allows for adding "format" validators which do work with angular-schema-form but do not result in useful error messages when the validation fails. This is a bug in the implementation of angular-schema-form as the error message returned from the tv4 format validator should be passed through angular-schema-form and rendered in the displayed error message on the form.

sergedomk avatar Jan 24 '16 18:01 sergedomk

Ok, I understand. If it is an simple fix, I would recommend you making a PR.

nicklasb avatar Jan 24 '16 19:01 nicklasb