Skip to content

Unexpected default error message for anchor's maxLength rule #4611

@Rua-Yuki

Description

@Rua-Yuki

Waterline version: 0.13.6
Node version: 10.14.2
NPM version: 6.4.1
Operating system: macOS 10.13.3


There appears to be a very small issue with the default message reported by anchor's maxLength rule. Specifically, with the changes introduced to improve validation error messages (sailshq/anchor@18e0e7f) in v1.4.0, it appears that the length overage is displayed as a negative number.

For example, it's possible to receive something like the following:

Could not use specified `lenTestField`.  Violated one or more validation rules:
  • Value was -4 characters longer than the configured maximum length (2)

The specific issue portion is the "-X characters longer" phrase, which doesn't make much sense.


This issue can be reproduced easily given the following model definition and usage code:

/**
 * @file ValidationTestModel.js
 */

module.exports = {

  attributes: {
    lenTestField: {
      type: 'string',
      maxLength: 2,
    },
  }

};
/**
 * Short and simple promise-based code that can be executed in the `sails console`,
 * for your convenience.
 */

ValidationTestModel.create({
    lenTestField: 'potato',
}).catch(err => {
    sails.log.error('Woops:', err);
});

The errant line of code appears to be:
https://github.com/sailshq/anchor/blob/18e0e7fd144f0246cb7cdad5d8501681dc52b41a/lib/rules.js#L262

The specific culprit being that the overage calculation arithmetic is backwards, with maxLength-x.length written rather than x.length-maxLength on two occasions.

As a side-effect of always being negative, the pluralisation logic intended to select between outputting the singular "character" or plural "characters" will not function as expected. Reordering both operations as mentioned above will solve this pluralisation issue bit as well.


My apologies for the long and pedantic issue report!

(Also I wasn't certain if I should file this under the main sails repo or this one – sorry if I've goofed up in this regard!)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelpful info or workaroundormRelated to models, datastores, orm config, Waterline, sails-hook-orm, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions