bogus34 / bookshelf-fields

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Messages not working/flexible

Smolations opened this issue · comments

I am having issues seeing any of the CheckIt-defined messages for rules. For instance, adding the field definition to a model:

[Fields.EmailField, 'contact_email', {required: true}]

and then attempting to set a bad email address produces failed validation, but with [something like]:

{
    checkit: {
        target: {
            id: 1,
            contact_email: 'invalid email',
            created_at: Tue Jun 10 2014 14: 24: 18 GMT - 0600(MDT),
            updated_at: Tue Jun 10 2014 14: 24: 18 GMT - 0600(MDT)
        },
        _applyAll: [],
        labels: {},
        labelTransform: [Function],
        async: true,
        language: 'en',
        _asyncError: [Function],
        validations: {
            contact_email: [Object]
        },
        errors: {
            contact_email: [Object]
        }
    },
    language: {
        labels: {},
        messages: {
            generic: 'Validation for {{label}} did not pass',
            exists: 'The {{label}} must be defined',
            required: 'The {{label}} is required',
            minLength: 'The {{label}} must be at least {{var_1}} characters long',
            maxLength: 'The {{label}} must not exceed {{var_1}} characters long',
            exactLength: 'The {{label}} must be exactly {{var_1}} characters long',
            lessThan: 'The {{label}} must contain a number less than {{var_1}}',
            lessThanEqualTo: 'The {{label}} must contain a number less than or equal to {{var_1}}',
            greaterThanEqualTo: 'The {{label}} must contain a number greater than or equal to {{var_1}}',
            validEmail: 'The {{label}} must contain a valid email address',
            isEqual: 'The {{label}} does not match {{var_1}}',
            isBoolean: 'The {{label}} must be type "boolean"',
            isEmpty: 'The {{label}} must be empty',
            isArray: 'The {{label}} must be an array',
            isNumeric: 'The {{label}} must contain only numbers',
            isInteger: 'The {{label}} must contain an integer',
            isFloat: 'The {{label}} must contain a floating point number',
            alpha: 'The {{label}} must only contain alphabetical characters',
            alphaDash: 'The {{label}} must only contain alpha-numeric characters, underscores, and dashes',
            alphaNumeric: 'The {{label}} must only contain alpha-numeric characters',
            alphaUnderscore: 'The {{label}} must only contain alpha-numeric characters, underscores, and dashes',
            natural: 'The {{label}} must contain only positive numbers',
            naturalNonZero: 'The {{label}} must contain a number greater than zero',
            ipv4: 'The {{label}} must contain a valid IPv4 string',
            base64: 'The {{label}} must contain a base64 string',
            luhn: 'The {{label}} must contain a valid credit card number',
            fallback: 'Validation for {{label}} did not pass'
        }
    },
    message: 'The validation failed with 1 errors.'
}

// response.checkit.errors
{ contact_email: [ { rule: 'validEmail', param: [], result: false } ] }

// response.checkit.validations.contact_email
[ { rule: 'required', param: [], result: true },
  { rule: 'validEmail', param: [], result: false } ]

After reading the CheckIt documentation, I can see that error messages can be specified for individual rules. Is this something your project cannot do yet? It appears that the entire language.(labels|messages) construct is being ignored.

I cannot find a way to specify a custom message through the API you have provided. Can you provide some insight as to how far along your project is (documentation in the README looks incomplete) and if what I'm trying to accomplish is possible with your library please?

Thanks!

Good point!
I'll update the library within a couple of days so it will be posible to add parameters to validation rules.

Hello!

Finally I've updated package and now you can pass advanced validation options to Checkit.
For example, you could write
@field StringField, 'password', mix_length: {value: 10, message: '{{label}} is too short to be valid!'}
or you can add complete Checkit rules to a field:
@field StringField 'username', validations: [{rule: 'minLength:5'}]

You also can pass message and label as a field options:
@field EmailField, 'email', message: '{{label}}: foo', label: 'foo'

And finally you can do something like that (coffeescript example):

db.Checkit.i18n.ru =
    labels: {}
    messages:
        email: 'Поле {{label}} должно содержать email-адрес'

class User extends db.Model
    tableName: 'users'
    @enable_validation(language: 'ru')
    @field F.EmailField, 'email'