Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validatejs shim doesn't respect '^' #20

Open
mickmcgrath13 opened this issue Apr 21, 2016 · 1 comment
Open

validatejs shim doesn't respect '^' #20

mickmcgrath13 opened this issue Apr 21, 2016 · 1 comment

Comments

@mickmcgrath13
Copy link

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
Copy link
Author

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...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant