ansman/validate.js

Prioritize error messages

Opened this issue · 0 comments

First, big like for this great library

I have a constraints object that every attribute has its own message. but some of the attr has multiple validators and respectively show all error messages of that attr if no validator passes.

What I want is how can I show only one error message at a time for every attribute?
It means for example in national_code, if presence and numericality didn't pass, at first, it only shows the presence message and if it pass, then check the numericality and show its message and at last check and show type message

const constraints = {
	national_code: {
		presence: {
			allowEmpty: false,
			message: 'is required'
		},
		numericality: {
			onlyInteger: true,
			message: '^only numeric'
		},
		type: {
			type: function(value) {
				return isValidNationalCode(value);
			},
			message: '^is not valid nation code'
		}
	},
	english_name: {
		presence: {
			allowEmpty: false,
			message: 'is required'
		},
		format: {
			pattern: /^[a-zA-Z]+$/,
			flags: 'i',
			message: '^only english character is allowed'
		}
	}
};