Skip to content

Commit

Permalink
Add Digits between rule (#326)
Browse files Browse the repository at this point in the history
* Add digits_between rule

* localization for digits_between rule

* update dist languages for digits_between rule

* comment typo fix
  • Loading branch information
vict-shevchenko authored and mikeerickson committed Aug 18, 2019
1 parent 1732a7d commit 5c8b10e
Show file tree
Hide file tree
Showing 52 changed files with 167 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,10 @@ The field under validation must be a valid date format which is acceptable by Ja

The field under validation must be numeric and must have an exact length of value.

#### digits_between:min,max

The field under validation must be numeric and must have length between given min and max.

#### different:attribute

The given field must be different than the field under validation.
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ar.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: 'الصفة :attribute صيغتها ليست تاريخ صحيح',
def: 'الصفة :attribute تحتوي على أخطاء',
digits: 'الصفة :attribute يجب أن تكون :digits أرقام.',
digits_between: 'يجب أن يحتوي :attribute بين :min و :max رقمًا/أرقام .',
different: 'الصفة :attribute و الصفة :different يجب أن تكونا مختلفتين',
'in': 'الصفة :attribute المختارة، غير صحيحة.',
integer: 'الصفة :attribute يجب أن تكون عدد صحيح',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
confirmed: 'La confirmació de :attribute no coincideix.',
different: 'El camp :attribute y :other poden ser diferents.',
digits: 'El camp :attribute pot tindre :digits dígitos.',
digits_between: 'El camp :attribute ha de tenir entre :min i :max dígits.',
email: 'El camp :attribute no es un correu válido.',
'in': 'El camp :attribute es invàlid.',
integer: 'El camp :attribute pot ser un nombre enter.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/da.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: ':attribute er ikke en gyldig dato.',
def: ':attribute attributen har fejl.',
digits: ':attribute skal have :digits cifre.',
digits_between: ':attribute skal have mellem :min og :max cifre.',
different: ':attribute og :different skal være forskellige.',
in: 'Det valgte :attribute er ugyldigt.',
integer: ':attribute skal være et heltal.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: 'Das :attribute Feld muss ein gültiges Datum sein.',
def: 'Das :attribute Feld hat Fehler.',
digits: 'Das :attribute Feld muss :digits Stellen haben.',
digits_between: 'Das :attribute Feld muss zwischen :min und :max Stellen haben.',
different: 'Die Felder :attribute und :different müssen sich unterscheiden.',
'in': 'Der gewählte Wert für :attribute ist ungültig.',
integer: 'Das :attribute Feld muss eine ganze Zahl sein.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/el.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module.exports = {
date: 'Το πεδίο :attribute δεν είναι έγκυρη ημερομηνία.',
def: 'Το πεδίο :attribute περιέχει σφάλματα.',
digits: 'Το πεδίο :attribute πρέπει να είναι :digits ψηφία.',
digits_between: 'Το πεδίο :attribute πρέπει να είναι μεταξύ :min και :max ψηφία.',
different: 'Το πεδίο :attribute και :different πρέπει να είναι διαφορετικά.',
'in': 'Το επιλεγμένο :attribute δεν είναι έγκυρο.',
integer: 'Το πεδίο :attribute πρέπει να είναι ακέραιος.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: 'The :attribute is not a valid date format.',
def: 'The :attribute attribute has errors.',
digits: 'The :attribute must be :digits digits.',
digits_between: 'The :attribute field must be between :min and :max digits.',
different: 'The :attribute and :different must be different.',
'in': 'The selected :attribute is invalid.',
integer: 'The :attribute must be an integer.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
confirmed: 'La confirmación de :attribute no coincide.',
different: 'El campo :attribute y :other deben ser diferentes.',
digits: 'El campo :attribute debe tener :digits dígitos.',
digits_between: 'El campo :attribute debe tener entre :min y :max dígitos.',
email: 'El campo :attribute no es un correo válido.',
'in': 'El campo :attribute es inválido.',
integer: 'El campo :attribute debe ser un número entero.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/fa.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: 'تاریخ درج شده در فیلد :attribute صحیح نیست',
def: 'فیلد :attribute اشکال دارد',
digits: 'فیلد :attribute می بایست شامل :digits رقم باشد',
digits_between: ':attribute باید بین :min و :max رقم باشد.',
different: 'فیلد :attribute می بایست مقداری غیر از :different داشته باشد',
'in': 'فیلد :attribute انتخاب شده صحیح نمی باشد',
integer: 'فیلد :attribute می بایست عددی باشد',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/fi.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: ':attribute ei ole päivämäärä.',
def: ':attribute sisältää virheitä.',
digits: ':attribute on oltava :digits numeroa pitkä.',
digits_between: 'Kentän :attribute arvon tulee olla :min - :max numeroa.',
different: ':attribute ei saa olla yhtä kuin :different.',
'in': 'Valittu :attribute ei kelpaa.',
integer: ':attribute ei ole numero.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/fr.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
email: 'Le champ :attribute contient un format invalide.',
def: 'Le champ :attribute contient un attribut erroné.',
digits: 'Le champ :attribute doit être composé de :digits chiffres.',
digits_between: 'Le champ :attribute doit contenir entre :min et :max chiffres.',
different: 'Les champs :attribute et :different doivent être différents.',
'in': 'Le champ :attribute est invalide.',
integer: 'Le champ :attribute doit être un entier.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/id.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: ':attribute format tanggal tidak benar.',
def: ':attribute attribute has errors.',
digits: ':attribute harus :digits digit.',
digits_between: 'Isian :attribute harus antara angka :min dan :max.',
different: ':attribute dan :different harus berbeda.',
'in': ':attribute tidak benar.',
integer: ':attribute harus berupa angka.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/it.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
email: 'Il formato dell\'attributo :attribute non è valido.',
def: 'Gli attributi del campo :attribute contengono degli errori.',
digits: 'Il campo :attribute deve essere di :digits cifre.',
digits_between: 'Il campo :attribute deve essere tra :min e :max cifre.',
different: 'Il campo :attribute e :different devo essere diversi.',
'in': 'Il valore del campo :attribute non è valido.',
integer: 'Il campo :attribute deve essere un valore intero.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ja.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: ':attributeは正しい日付形式を入力してください',
def: ':attributeは検証エラーが含まれています。',
digits: ':attributeは:digitsの数字のみで入力してください。',
digits_between: ':attributeは、:min桁から:max桁にしてください。',
different: ':attributeと:differentは同じであってはなりません。',
'in': '選択された:attributeは無効です。',
integer: ':attributeは整数で入力してください。',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/nb_NO.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: ':attribute er et ugyldig datoformat.',
def: ':attribute attributtet har feil.',
digits: ':attribute må være på :digits siffer.',
digits_between: ':attribute må være mellom :min og :max siffer.',
different: ':attribute og :different må være forskjellige.',
'in': 'Den oppgitte verdien for :attribute er ugyldig.',
integer: ':attribute må være et heltall.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/nl.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: 'Het :attribute veld moet een geldige datum zijn.',
def: 'Het :attribute veld bevat fouten.',
digits: 'Het :attribute veld moet :digits cijfers hebben.',
digits_between: ':attribute moet bestaan uit minimaal :min en maximaal :max cijfers.',
different: 'Het :attribute en :different veld moeten verschillend zijn.',
'in': 'De gekozen waarde voor :attribute is ongeldig.',
integer: 'Het :attribute veld moet een geheel getal zijn.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/pl.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: 'Pole :attribute musi mieć poprawny format daty.',
def: 'Pole :attribute zawiera błędy.',
digits: 'Pole :attribute może zawierać tylko cyfry ze zbioru :digits.',
digits_between: 'Pole :attribute musi mieć od :min do :max cyfr.',
different: 'Pola :attribute i :different muszą się różnić.',
'in': 'Pole :attribute musi należeć do zbioru :in.',
integer: 'Pole :attribute musi być liczbą całkowitą.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ro.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module.exports = {
date: ':attribute nu este un format de dată valid.',
def: 'Atributul :attribute are erori.',
digits: ':attribute trebuie să aibă :digits cifre.',
digits_between: 'Câmpul :attribute trebuie să aibă între :min și :max cifre.',
different: ':attribute și :different trebuie sa fie diferite.',
'in': 'Atributul selectat :attribute nu este valid.',
integer: ':attribute trebuie să fie un număr întreg.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ru.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
email: 'Поле :attribute должно быть действительным электронным адресом.',
def: 'Поле :attribute содержит ошибки.',
digits: 'Длина цифрового поля :attribute должна быть :digits.',
digits_between: 'Длинна цифрового поля :attribute должна быть от :min до :max знаков.',
different: 'Поля :attribute и :different должны различаться.',
'in': 'Выбранное значение для :attribute ошибочно.',
integer: 'Поле :attribute должно быть целым числом.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/tr.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: ':attribute geöerli bir tarih alanı değil.',
def: ':attribute hatalar içeriyor.',
digits: ':attribute sadece rakamlardan oluşabilir.',
digits_between: ':attribute :min ile :max arasında rakam olmalıdır.',
different: ':attribute ve :different farklı olmalı.',
'in': 'Seçilen :attribute geçerli değil.',
integer: ':attribute tam sayı olmalı.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/ua.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
email: 'Значення поля :attribute повинно бути існуючою електронною адресою.',
def: 'Поле :attribute містить помилки.',
digits: 'Довжина числового поля :attribute повинна бути :digits.',
digits_between: 'Довжина цифрового поля :attribute повинна бути від :min до :max.',
different: 'Поля :attribute і :different повинні відрізнятись.',
'in': 'Обране значення для :attribute помилкове.',
integer: 'Значення поля :attribute повинно бути цілим числом.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/vi.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: ':attribute không phải là ngày hợp lệ',
def: 'Thuộc tính :attribute có lỗi.',
digits: ':attribute phải là số và có chiều dài bằng :digits.',
digits_between: 'Độ dài của trường :attribute phải nằm trong khoảng :min and :max chữ số.',
different: 'Giá trị của hai trường :attribute và :different phải khác nhau.',
'in': 'Giá trị được chọn của :attribute không hợp lệ.',
integer: ':attribute phải là số nguyên.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/zh.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: ':attribute日期格式错误.',
def: ':attribute属性错误.',
digits: ':attribute必须是:digits位小数.',
digits_between: ':attribute 必须是介于 :min 和 :max 位的数字。',
different: ':attribute和:different必须不同.',
'in': '选择的:attribute无效',
integer: ':attribute必须是一个整数.',
Expand Down
1 change: 1 addition & 0 deletions dist/lang/zh_TW.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: ':attribute的日期格式錯誤。',
def: ':attribute屬性錯誤。',
digits: ':attribute必須是:digits位小數。',
digits_between: ':attribute 必須介於 :min 至 :max 位數字。',
different: ':attribute和:different必須不同。',
'in': '選擇的:attribute無效',
integer: ':attribute必須是一個整數。',
Expand Down
32 changes: 31 additions & 1 deletion dist/validator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*! validatorjs - v3.15.0 - - 2018-10-26 */
/*! validatorjs - v3.15.0 - - 2019-03-28 */
(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Validator = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
function AsyncResolvers(onFailedOne, onResolvedAll) {
this.onResolvedAll = onResolvedAll;
Expand Down Expand Up @@ -100,6 +100,21 @@ var replacements = {
});
},

/**
* Digits-Between replacement (replaces :min and :max)
*
* @param {string} template
* @param {Rule} rule
* @return {string}
*/
digits_between: function(template, rule) {
var parameters = rule.getParameters();
return this._replacePlaceholders(rule, template, {
min: parameters[0],
max: parameters[1]
});
},

/**
* Required_if replacement.
*
Expand Down Expand Up @@ -443,6 +458,7 @@ module.exports = {
date: 'The :attribute is not a valid date format.',
def: 'The :attribute attribute has errors.',
digits: 'The :attribute must be :digits digits.',
digits_between: 'The :attribute field must be between :min and :max digits.',
different: 'The :attribute and :different must be different.',
'in': 'The selected :attribute is invalid.',
integer: 'The :attribute must be an integer.',
Expand Down Expand Up @@ -940,6 +956,20 @@ var rules = {
return false;
},

digits_between: function(val) {
var numericRule = this.validator.getRule('numeric');
var req = this.getParameters();
var valueDigitsCount = String(val).length;
var min = parseFloat(req[0], 10);
var max = parseFloat(req[1], 10);

if (numericRule.validate(val) && valueDigitsCount >= min && valueDigitsCount <= max) {
return true;
}

return false;
},

regex: function(val, req) {
var mod = /[g|i|m]{1,3}$/;
var flag = req.match(mod);
Expand Down
2 changes: 1 addition & 1 deletion dist/validator.min.js

Large diffs are not rendered by default.

56 changes: 56 additions & 0 deletions spec/digits_between-rule.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
if (typeof require !== 'undefined') {
var Validator = require('../src/validator.js');
var expect = require('chai').expect;
} else {
var Validator = window.Validator;
var expect = window.chai.expect;
}

describe('digits_between rule', function() {
it('should be numeric and must have an exact length between 8 and 10', function() {
var validation = new Validator({
phone: '800692775'
}, {
phone: 'digits_between:8,10'
});

expect(validation.passes()).to.be.true;
expect(validation.fails()).to.be.false;
});

it('should not pass if non-digits are present', function() {
var validation = new Validator({
phone: '80069277a'
}, {
phone: 'digits_between:8,10'
});

expect(validation.fails()).to.be.true;
expect(validation.errors.first('phone')).to.equal('The phone field must be between 8 and 10 digits.');
expect(validation.passes()).to.be.false;
});

it('should not pass if value length is below minimum', function() {
var validation = new Validator({
phone: '8006927'
}, {
phone: 'digits_between:8,10'
});

expect(validation.fails()).to.be.true;
expect(validation.errors.first('phone')).to.equal('The phone field must be between 8 and 10 digits.');
expect(validation.passes()).to.be.false;
});

it('should not pass if value length is above maximum', function() {
var validation = new Validator({
phone: '80069277525'
}, {
phone: 'digits_between:8,10'
});

expect(validation.fails()).to.be.true;
expect(validation.errors.first('phone')).to.equal('The phone field must be between 8 and 10 digits.');
expect(validation.passes()).to.be.false;
});
});
15 changes: 15 additions & 0 deletions src/attributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,21 @@ var replacements = {
});
},

/**
* Digits-Between replacement (replaces :min and :max)
*
* @param {string} template
* @param {Rule} rule
* @return {string}
*/
digits_between: function(template, rule) {
var parameters = rule.getParameters();
return this._replacePlaceholders(rule, template, {
min: parameters[0],
max: parameters[1]
});
},

/**
* Required_if replacement.
*
Expand Down
1 change: 1 addition & 0 deletions src/lang/ar.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
date: 'الصفة :attribute صيغتها ليست تاريخ صحيح',
def: 'الصفة :attribute تحتوي على أخطاء',
digits: 'الصفة :attribute يجب أن تكون :digits أرقام.',
digits_between: 'يجب أن يحتوي :attribute بين :min و :max رقمًا/أرقام .',
different: 'الصفة :attribute و الصفة :different يجب أن تكونا مختلفتين',
'in': 'الصفة :attribute المختارة، غير صحيحة.',
integer: 'الصفة :attribute يجب أن تكون عدد صحيح',
Expand Down
1 change: 1 addition & 0 deletions src/lang/ca.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
confirmed: 'La confirmació de :attribute no coincideix.',
different: 'El camp :attribute y :other poden ser diferents.',
digits: 'El camp :attribute pot tindre :digits dígitos.',
digits_between: 'El camp :attribute ha de tenir entre :min i :max dígits.',
email: 'El camp :attribute no es un correu válido.',
'in': 'El camp :attribute es invàlid.',
integer: 'El camp :attribute pot ser un nombre enter.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/da.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
date: ':attribute er ikke en gyldig dato.',
def: ':attribute attributen har fejl.',
digits: ':attribute skal have :digits cifre.',
digits_between: ':attribute skal have mellem :min og :max cifre.',
different: ':attribute og :different skal være forskellige.',
in: 'Det valgte :attribute er ugyldigt.',
integer: ':attribute skal være et heltal.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/de.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
date: 'Das :attribute Feld muss ein gültiges Datum sein.',
def: 'Das :attribute Feld hat Fehler.',
digits: 'Das :attribute Feld muss :digits Stellen haben.',
digits_between: 'Das :attribute Feld muss zwischen :min und :max Stellen haben.',
different: 'Die Felder :attribute und :different müssen sich unterscheiden.',
'in': 'Der gewählte Wert für :attribute ist ungültig.',
integer: 'Das :attribute Feld muss eine ganze Zahl sein.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/el.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module.exports = {
date: 'Το πεδίο :attribute δεν είναι έγκυρη ημερομηνία.',
def: 'Το πεδίο :attribute περιέχει σφάλματα.',
digits: 'Το πεδίο :attribute πρέπει να είναι :digits ψηφία.',
digits_between: 'Το πεδίο :attribute πρέπει να είναι μεταξύ :min και :max ψηφία.',
different: 'Το πεδίο :attribute και :different πρέπει να είναι διαφορετικά.',
'in': 'Το επιλεγμένο :attribute δεν είναι έγκυρο.',
integer: 'Το πεδίο :attribute πρέπει να είναι ακέραιος.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/en.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ module.exports = {
date: 'The :attribute is not a valid date format.',
def: 'The :attribute attribute has errors.',
digits: 'The :attribute must be :digits digits.',
digits_between: 'The :attribute field must be between :min and :max digits.',
different: 'The :attribute and :different must be different.',
'in': 'The selected :attribute is invalid.',
integer: 'The :attribute must be an integer.',
Expand Down
1 change: 1 addition & 0 deletions src/lang/es.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
confirmed: 'La confirmación de :attribute no coincide.',
different: 'El campo :attribute y :other deben ser diferentes.',
digits: 'El campo :attribute debe tener :digits dígitos.',
digits_between: 'El campo :attribute debe tener entre :min y :max dígitos.',
email: 'El campo :attribute no es un correo válido.',
'in': 'El campo :attribute es inválido.',
integer: 'El campo :attribute debe ser un número entero.',
Expand Down
Loading

0 comments on commit 5c8b10e

Please sign in to comment.