canjs / can-validate

Validation utilities to be used with can-validate plugins.
https://canjs.com/doc/can-validate.html
MIT License
1 stars 4 forks source link

validatejs shim doesn't respect '^' #20

Open mickmcgrath13 opened 8 years ago

mickmcgrath13 commented 8 years ago

The validatejs docs say:

If you need an error not to be prefixed by the attribute add a leading ^ to the error and it won't be prepended. If you need to have a leading ^ but want the prefixing just write ^.

However, this doesn't work with can-validate because of the use of validatejs.single and the manual prepending of the key.

        var errors = validatejs.single(value, processOptions(options));

        // Add the name to the front of the error string
        if (errors && name) {
            for (var i = 0; i < errors.length; i++) {
                // Attempt to prettyify the name in each error
                errors[i] = can.capitalize(can.camelize(name)) + ' ' + errors[i];
            }
        }

        return errors;

Validate.js's single method forces the fullMessages to be false

    single: function(value, constraints, options) {
      options = v.extend({}, v.single.options, options, {
        format: "flat",
        fullMessages: false
      });
      return v({single: value}, {single: constraints}, options);
    },

...which means that it isn't taking advantage of its internal name prettifying.

        if (error[0] === '^') {
          error = error.slice(1);
        } else if (options.fullMessages !== false) {
          error = v.capitalize(v.prettify(errorInfo.attribute)) + " " + error;
        }
        error = error.replace(/\\\^/g, "^");
        error = v.format(error, {value: v.stringifyValue(errorInfo.value)});
        ret.push(v.extend({}, errorInfo, {error: error}));

Why not just use a non validatejs.single method and let validatejs do its own internal pretty naming and allow users to take advantage of the "^" functionality?

mickmcgrath13 commented 8 years ago

I have a branch that should address this: https://github.com/canjs/can-validate/tree/issue-20 The relevant code is here: https://github.com/canjs/can-validate/blob/issue-20/can-validate/shims/validatejs.shim.js#L34 ...and there are tests: https://github.com/canjs/can-validate/blob/issue-20/can-validate/shims/validatejs.test.js

However, I haven't been able to test the tests because my local npm install doesn't work... I'll create another issue for that...