Comparing version 1.2.1 to 1.2.2
## Changelog | ||
### 1.2.2 | ||
Add decimal string validation | ||
### 1.2.1 | ||
@@ -4,0 +8,0 @@ |
@@ -28,2 +28,3 @@ 'use strict'; | ||
'string.numeric': '{{#label}} must only contain numeric characters', | ||
'string.decimal': '{{#label}} must be a decimal number of maximum {{#digit}} digits and {{#decimal}} decimal places', | ||
'string.base32': '{{#label}} must be a valid base32 string', | ||
@@ -121,2 +122,33 @@ 'string.countryCode': '{{#label}} must be a valid ISO {{#type}} country code', | ||
}, | ||
decimal: { | ||
method(digit, decimal) { | ||
return this.$_addRule({ name: 'decimal', args: { digit, decimal } }); | ||
}, | ||
args: [ | ||
{ | ||
ref: true, | ||
name: 'digit', | ||
assert: (value) => typeof value === 'number', | ||
message: `must be a number` | ||
}, | ||
{ | ||
ref: true, | ||
name: 'decimal', | ||
assert: (value) => typeof value === 'number', | ||
message: `must be a number` | ||
} | ||
], | ||
validate: (value, helpers, args, options) => { | ||
let digit = args.digit; | ||
let decimal = args.decimal; | ||
let pattern = `^(0(\\.\\d{1,${decimal}})?|([1-9]\\d{0,${digit - decimal - 1}}(\\.\\d{1,${decimal}})?))$`; | ||
let regex = new RegExp(pattern); | ||
if (regex.test(value)) { | ||
return value; | ||
} | ||
return helpers.error('string.decimal', { digit, decimal }); | ||
} | ||
}, | ||
base32: { | ||
@@ -123,0 +155,0 @@ validate: (value, helpers, args, options) => { |
{ | ||
"name": "joi-plus", | ||
"version": "1.2.1", | ||
"version": "1.2.2", | ||
"description": "Joi with extra rules for string and array.", | ||
@@ -20,2 +20,3 @@ "repository": "git://github.com/flamehamster/joi-plus", | ||
"numeric", | ||
"decimal", | ||
"base32", | ||
@@ -22,0 +23,0 @@ "password", |
@@ -26,2 +26,5 @@ # Joi-Plus | ||
* Joi.string().decimal(digit, decimal) | ||
* Requires the string value to be a valid decimal number. | ||
* Joi.string().base32() | ||
@@ -109,2 +112,6 @@ * Requires the value to be a valid base32 string. | ||
.required() | ||
salary: Joi.string() | ||
.decimal(11,2) | ||
.required() | ||
}); | ||
@@ -145,2 +152,6 @@ ``` | ||
* `salary` | ||
* a required string | ||
* must be a valid decimal number with up to 11 digits with 2 decimal places | ||
#### Sanitize | ||
@@ -147,0 +158,0 @@ Using Joi.string().sanitize() with sanitization libraries such as [sanitize-html](https://www.npmjs.com/package/sanitize-html) |
19911
345
185