@patterninc/laravel_js_validator
Advanced tools
Comparing version 1.0.14 to 1.0.17
@@ -118,8 +118,28 @@ "use strict"; | ||
}); | ||
test('fails for array datatype with min validation', () => { | ||
const results = index_1.Validator.validate([], 'array|minItems:1'); | ||
test('passes for string valuetype with minItems and maxItems validation', () => { | ||
const results = index_1.Validator.validate('a,b,c', 'array|minItems:1|maxItems:4'); | ||
expect(results.success).toBe(true); | ||
}); | ||
test('passes for boolean datatype option validation', () => { | ||
const results = index_1.Validator.validate(true, 'in:false,true'); | ||
const results2 = index_1.Validator.validate(false, 'in:false,true'); | ||
expect(results.success).toBe(true); | ||
expect(results2.success).toBe(true); | ||
}); | ||
test('passes for empty value option validation', () => { | ||
const results = index_1.Validator.validate(null, 'in:abc,pqr'); | ||
const results2 = index_1.Validator.validate('', 'in:abc,pqr'); | ||
expect(results.success).toBe(true); | ||
expect(results2.success).toBe(true); | ||
}); | ||
test('passes for array value option validation', () => { | ||
const results = index_1.Validator.validate(['abc'], 'array|in:abc,pqr'); | ||
expect(results.success).toBe(true); | ||
}); | ||
test('fails for empty array value option validation', () => { | ||
const results = index_1.Validator.validate([''], 'array|in:abc,pqr'); | ||
expect(results.success).toBe(false); | ||
expect(results.errors).toEqual({ | ||
t: ['The t must have at least 1 item(s).'], | ||
t: ['The selected t is invalid.'], | ||
}); | ||
}); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.IgnoredRules = void 0; | ||
exports.IgnoredRules = ['precision']; | ||
exports.IgnoredRules = ['precision', 'array', 'minItems', 'maxItems']; |
@@ -23,3 +23,5 @@ 'use strict'; | ||
typeof data === 'number' || | ||
data instanceof Array) && | ||
typeof data === 'boolean' || | ||
data instanceof Array || | ||
data === null) && | ||
typeof rules === 'string') { | ||
@@ -40,4 +42,4 @@ validator = new Validator({ t: data }, { t: rules }); | ||
return { | ||
success: validator.passes(), | ||
errors: validator.errors, | ||
success: validator ? validator.passes() : true, | ||
errors: validator ? validator.errors : {}, | ||
}; | ||
@@ -44,0 +46,0 @@ } |
@@ -13,4 +13,4 @@ "use strict"; | ||
numeric: 'The :attr must be between :0 and :1.', | ||
string: 'The :attr must be between :0 and :1 characters.', | ||
array: 'The :attr must have between :0 and :1 items.', | ||
string: 'The :attr must be between :0 and :1 character(s).', | ||
array: 'The :attr must have between :0 and :1 item(s).', | ||
}, | ||
@@ -26,9 +26,9 @@ boolean: 'The :attr field must be true or false.', | ||
numeric: 'The :attr may not be greater than :0.', | ||
string: 'The :attr may not be greater than :0 characters.', | ||
array: 'The :attr may not have more than :0 items.', | ||
string: 'The :attr may not be greater than :0 character(s).', | ||
array: 'The :attr may not have more than :0 item(s).', | ||
}, | ||
min: { | ||
numeric: 'The :attr must be at least :0.', | ||
string: 'The :attr must be at least :0 characters.', | ||
array: 'The :attr must have at least :0 items.', | ||
string: 'The :attr must be at least :0 character(s).', | ||
array: 'The :attr must have at least :0 item(s).', | ||
}, | ||
@@ -45,4 +45,4 @@ minitems: 'The :attr must have at least :0 item(s).', | ||
numeric: 'The :attr must be :0.', | ||
string: 'The :attr must be :0 characters.', | ||
array: 'The :attr must contain :0 items.', | ||
string: 'The :attr must be :0 character(s).', | ||
array: 'The :attr must contain :0 item(s).', | ||
}, | ||
@@ -49,0 +49,0 @@ string: 'The :attr must be a string.', |
@@ -103,2 +103,5 @@ "use strict"; | ||
validateIn(name, value, params) { | ||
if (value === null || value === undefined || value === '') { | ||
return true; | ||
} | ||
if (Array.isArray(value) && this.validationUtils.hasRule(name, 'Array')) { | ||
@@ -108,4 +111,16 @@ let arr = this.arrayDiff(value, params); | ||
} | ||
else if (typeof value === 'boolean') { | ||
return params.map(this.parseBoolean).indexOf(value) >= 0; | ||
} | ||
else if (typeof value === 'number') { | ||
return params.map(Number).indexOf(value) >= 0; | ||
} | ||
return params.indexOf(value) >= 0; | ||
} | ||
parseBoolean(value) { | ||
if (typeof value === 'string') { | ||
value = value.toLowerCase(); | ||
} | ||
return ['true', '1', 'yes'].includes(value); | ||
} | ||
arrayDiff(arr1, arr2) { | ||
@@ -112,0 +127,0 @@ let diff = []; |
@@ -1,1 +0,1 @@ | ||
export const IgnoredRules = ['precision']; | ||
export const IgnoredRules = ['precision', 'array', 'minItems', 'maxItems']; |
@@ -21,3 +21,5 @@ 'use strict'; | ||
typeof data === 'number' || | ||
data instanceof Array) && | ||
typeof data === 'boolean' || | ||
data instanceof Array || | ||
data === null) && | ||
typeof rules === 'string') { | ||
@@ -38,4 +40,4 @@ validator = new Validator({ t: data }, { t: rules }); | ||
return { | ||
success: validator.passes(), | ||
errors: validator.errors, | ||
success: validator ? validator.passes() : true, | ||
errors: validator ? validator.errors : {}, | ||
}; | ||
@@ -42,0 +44,0 @@ } |
@@ -10,4 +10,4 @@ export const validationMessages = { | ||
numeric: 'The :attr must be between :0 and :1.', | ||
string: 'The :attr must be between :0 and :1 characters.', | ||
array: 'The :attr must have between :0 and :1 items.', | ||
string: 'The :attr must be between :0 and :1 character(s).', | ||
array: 'The :attr must have between :0 and :1 item(s).', | ||
}, | ||
@@ -23,9 +23,9 @@ boolean: 'The :attr field must be true or false.', | ||
numeric: 'The :attr may not be greater than :0.', | ||
string: 'The :attr may not be greater than :0 characters.', | ||
array: 'The :attr may not have more than :0 items.', | ||
string: 'The :attr may not be greater than :0 character(s).', | ||
array: 'The :attr may not have more than :0 item(s).', | ||
}, | ||
min: { | ||
numeric: 'The :attr must be at least :0.', | ||
string: 'The :attr must be at least :0 characters.', | ||
array: 'The :attr must have at least :0 items.', | ||
string: 'The :attr must be at least :0 character(s).', | ||
array: 'The :attr must have at least :0 item(s).', | ||
}, | ||
@@ -42,4 +42,4 @@ minitems: 'The :attr must have at least :0 item(s).', | ||
numeric: 'The :attr must be :0.', | ||
string: 'The :attr must be :0 characters.', | ||
array: 'The :attr must contain :0 items.', | ||
string: 'The :attr must be :0 character(s).', | ||
array: 'The :attr must contain :0 item(s).', | ||
}, | ||
@@ -46,0 +46,0 @@ string: 'The :attr must be a string.', |
@@ -100,2 +100,5 @@ import { ValidationUtils } from './ValidationUtils.js'; | ||
validateIn(name, value, params) { | ||
if (value === null || value === undefined || value === '') { | ||
return true; | ||
} | ||
if (Array.isArray(value) && this.validationUtils.hasRule(name, 'Array')) { | ||
@@ -105,4 +108,16 @@ let arr = this.arrayDiff(value, params); | ||
} | ||
else if (typeof value === 'boolean') { | ||
return params.map(this.parseBoolean).indexOf(value) >= 0; | ||
} | ||
else if (typeof value === 'number') { | ||
return params.map(Number).indexOf(value) >= 0; | ||
} | ||
return params.indexOf(value) >= 0; | ||
} | ||
parseBoolean(value) { | ||
if (typeof value === 'string') { | ||
value = value.toLowerCase(); | ||
} | ||
return ['true', '1', 'yes'].includes(value); | ||
} | ||
arrayDiff(arr1, arr2) { | ||
@@ -109,0 +124,0 @@ let diff = []; |
{ | ||
"name": "@patterninc/laravel_js_validator", | ||
"version": "1.0.14", | ||
"version": "1.0.17", | ||
"description": "Validate values against Laravel validation rules", | ||
@@ -32,4 +32,3 @@ "scripts": { | ||
"publishConfig": { | ||
"@patterninc:registry": "https://npm.pkg.github.com", | ||
"access": "public" | ||
"@patterninc:registry": "https://registry.npmjs.org/" | ||
}, | ||
@@ -46,2 +45,2 @@ "jest": { | ||
] | ||
} | ||
} |
@@ -99,4 +99,4 @@ ### Laravel style validators in JS | ||
numeric: 'The :attr must be between :min and :max.', | ||
string: 'The :attr must be between :min and :max characters.', | ||
array: 'The :attr must have between :min and :max items.', | ||
string: 'The :attr must be between :min and :max character(s).', | ||
array: 'The :attr must have between :min and :max item(s).', | ||
}, | ||
@@ -112,9 +112,9 @@ boolean: 'The :attr field must be true or false.', | ||
numeric: 'The :attr may not be greater than :max.', | ||
string: 'The :attr may not be greater than :max characters.', | ||
array: 'The :attr may not have more than :max items.', | ||
string: 'The :attr may not be greater than :max character(s).', | ||
array: 'The :attr may not have more than :max item(s).', | ||
}, | ||
min: { | ||
numeric: 'The :attr must be at least :min.', | ||
string: 'The :attr must be at least :min characters.', | ||
array: 'The :attr must have at least :min items.', | ||
string: 'The :attr must be at least :min character(s).', | ||
array: 'The :attr must have at least :min item(s).', | ||
}, | ||
@@ -128,4 +128,4 @@ not_in: 'The selected :attr is invalid.', | ||
numeric: 'The :attr must be :size.', | ||
string: 'The :attr must be :size characters.', | ||
array: 'The :attr must contain :size items.', | ||
string: 'The :attr must be :size character(s).', | ||
array: 'The :attr must contain :size item(s).', | ||
}, | ||
@@ -132,0 +132,0 @@ string: 'The :attr must be a 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
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
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
60857
1628
1