Comparing version 1.1.2 to 1.1.3
'use strict'; | ||
const Joi = require('@hapi/joi'); | ||
module.exports = Joi.extend( | ||
const Joi = require('@hapi/joi').extend( | ||
require('./libraries/string'), | ||
require('./libraries/array') | ||
); | ||
); | ||
module.exports = Joi; |
@@ -25,3 +25,4 @@ 'use strict'; | ||
'string.countryCode': '"{{#label}}" must be a valid ISO {{#type}} country code', | ||
'string.password': '"{{#label}}" {{#message}}' | ||
'string.password': '"{{#label}}" {{#message}}', | ||
'string.contain': '"{{#label}}" must contain "{{#seed}}"{{#message}}' | ||
}, | ||
@@ -129,2 +130,3 @@ coerce(value, helpers) { | ||
{ | ||
ref: true, | ||
name: 'type', | ||
@@ -162,2 +164,3 @@ assert: (value) => value === 'alpha-2' || value === 'alpha-3', | ||
let message = 'must contains at least'; | ||
if (rules.lowercase) { | ||
@@ -200,2 +203,41 @@ regex += '(?=.*[a-z])'; | ||
} | ||
}, | ||
contain: { | ||
method(seed, index) { | ||
return this.lowercase().$_addRule({ name: 'contain', args: { seed, index } }); | ||
}, | ||
args: [ | ||
{ | ||
ref: true, | ||
name: 'seed', | ||
assert: (value) => typeof value === 'string', | ||
message: `must be a string` | ||
}, | ||
{ | ||
ref: true, | ||
name: 'index', | ||
assert: (value) => value === undefined || typeof value === 'number' && value >= -1, | ||
message: `must be equal or more than -1` | ||
} | ||
], | ||
validate: (value, helpers, args, options) => { | ||
let seed = args.seed; | ||
let index = args.index; | ||
if (index) { | ||
if (index === -1) { | ||
index = value.length - seed.length; | ||
} | ||
if (value.indexOf(seed, index) === index) { | ||
return value; | ||
} | ||
let message = ` at index ${index}`; | ||
return helpers.error('string.contain', { seed, message }); | ||
} else { | ||
if (value.includes(seed)) { | ||
return value; | ||
} | ||
return helpers.error('string.contain', { seed }); | ||
} | ||
} | ||
} | ||
@@ -202,0 +244,0 @@ } |
{ | ||
"name": "joi-plus", | ||
"version": "1.1.2", | ||
"version": "1.1.3", | ||
"description": "Joi with extra rules for string and array.", | ||
@@ -5,0 +5,0 @@ "repository": "git://github.com/flamehamster/joi-plus", |
# Joi-Plus | ||
[![npm package](https://img.shields.io/npm/v/joi-plus.svg)](http://npmjs.org/package/joi-plus) [![npm license](https://img.shields.io/npm/l/joi-plus)](https://img.shields.io/npm/l/joi-plus) [![install size](https://packagephobia.now.sh/badge?p=joi-plus@1.1.2)](https://packagephobia.now.sh/result?p=joi-plus@1.1.2) [![dependencies status](https://david-dm.org/flamehamster/joi-plus/status.svg)](https://david-dm.org/flamehamster/joi-plus) | ||
[@hapi/joi - v16.1.8](https://www.npmjs.com/package/@hapi/joi) | ||
@@ -37,2 +39,7 @@ Making the most powerful schema description language and data validator for JavaScript slightly more powerful. | ||
* Joi.string().contain(seed, [index]) | ||
* Requires the string value to contain the seed. | ||
* If index is defined, position of the seed in the string must match the index. | ||
* Set index to -1 to match from end of string. | ||
* Joi.array().inList(list, [label]) | ||
@@ -58,2 +65,8 @@ * Requires the value in array to match the list. | ||
const schema = Joi.object({ | ||
username: Joi.string() | ||
.min(8) | ||
.max(20) | ||
.alpha() | ||
.required(), | ||
email: Joi.string() | ||
@@ -78,8 +91,2 @@ .email() | ||
username: Joi.string() | ||
.min(2) | ||
.max(20) | ||
.alpha() | ||
.required(), | ||
base32_encoded: Joi.string() | ||
@@ -106,2 +113,6 @@ .base32() | ||
The above schema defines the following constraints: | ||
* `username` | ||
* a required string | ||
* at least 8 characters long but no more than 20 | ||
* must contain only alphabetic characters | ||
* `email` | ||
@@ -122,6 +133,2 @@ * a required string | ||
* will be removed after validation | ||
* `username` | ||
* a required string | ||
* at least 8 characters long but no more than 20 | ||
* must contain only alphabetic characters | ||
* `base32_encoded` | ||
@@ -128,0 +135,0 @@ * a required string |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17374
8
308
174