@sumor/validator
Advanced tools
Comparing version 1.0.7 to 1.1.0
{ | ||
"name": "@sumor/validator", | ||
"description": "This is a lightweight validator for Node.JS. It can validate the input string or number based on the rules you defined.", | ||
"version": "1.0.7", | ||
"version": "1.1.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "repository": "sumor-cloud/validator", |
201
README.md
@@ -39,2 +39,77 @@ # validator | ||
### Basic Usage | ||
```js | ||
import { validate, format } from '@sumor/validator' | ||
const parameterDefinition = { | ||
// please refer to the following section for the detail | ||
rule: [ | ||
{ | ||
code: 'ONLY_CHAR_DIGIT', | ||
expression: '^[a-zA-Z0-9]*$', | ||
message: 'only allow a-z, A-Z, 0-9' | ||
} | ||
] | ||
} | ||
// validate the input, return the error message | ||
const messages = validate(parameterDefinition, 'de1234567') | ||
console.log(messages) // [ 'only allow a-z, A-Z, 0-9' ] | ||
// format the input, return the formatted value | ||
const value = format(parameterDefinition, ' demo ') | ||
console.log(value) // will print "demo", useless space will be removed | ||
``` | ||
### Parameter Definition | ||
- [optional] `type` data type, support `string`, `number`,`array`,`file`, default is `string` | ||
- [optional] `required` required or not, default is false | ||
- [optional] `length` length of the string, default is 0 means no limit | ||
- [optional] `default` default value, default is null | ||
- [optional] `rule` rule for the validation, default is empty array, please refer to the following section for the detail | ||
- [optional] `trim` trim the string or not, default is true for string type | ||
- [optional] `lowerCase` convert the string to lowercase or not, default is false | ||
- [optional] `upperCase` convert the string to uppercase or not, default is false | ||
- [optional] `decimal` decimal for the number, default is 0 means no decimal | ||
- [optional] `i18n` i18n for the error message, default is empty object, please refer to the following section for the detail | ||
- [optional] `error` output error or not, default is false | ||
for array type, `rule`, `default`, `trim`, `lowerCase`, `upperCase`, `decimal` are working for each item in the array. | ||
for file type, `rule` is working for the file object. | ||
### Rule Definition | ||
- `code` identifier for the rule, translate message based on this code | ||
- `message` original message for the rule | ||
- [optional] `expression` rule expression, should be a regular expression | ||
- [optional] `function` rule function, should be a function, return true if pass the rule | ||
```js | ||
// argument for the function | ||
// value: the value for the parameter | ||
// info: the parameter definition | ||
// language: current language | ||
;(value, info, language) => { | ||
return value > 5 | ||
} | ||
``` | ||
### I18n Definition | ||
Structure should be like the following: | ||
```json | ||
{ | ||
"language": { | ||
"code": "message" | ||
} | ||
} | ||
``` | ||
- `language` language code, support `en`, `zh`, `zh-TW` and so on | ||
- `code` identifier for the rule, should be the same as the rule code | ||
- `message` translated message for the rule | ||
### Validate String Usage | ||
@@ -45,3 +120,3 @@ | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'string', | ||
@@ -66,3 +141,3 @@ required: true, | ||
code: 'LENGTH_GREATER_THAN_5', | ||
expression: value => { | ||
function: value => { | ||
return value.length > 5 | ||
@@ -87,23 +162,23 @@ }, | ||
const messages1 = validate(parameterInfo, 'demo123456') | ||
const messages1 = validate(parameterDefinition, 'demo123456') | ||
console.log(messages1) // [] | ||
const messages2 = validate(parameterInfo, 'de1234567') | ||
const messages2 = validate(parameterDefinition, 'de1234567') | ||
console.log(messages2) // [ 'only allow a-z, A-Z, 0-9' ] | ||
const messages3 = validate(parameterInfo, 'demo!') | ||
const messages3 = validate(parameterDefinition, 'demo!') | ||
console.log(messages3) // [ 'only allow a-z, A-Z, 0-9', 'need include demo' ] | ||
const messages4 = validate(parameterInfo, 'de!mo') | ||
const messages4 = validate(parameterDefinition, 'de!mo') | ||
console.log(messages4) // [ 'only allow a-z, A-Z, 0-9', 'need include demo' ] | ||
const messages5 = validate(parameterInfo, 'de') | ||
const messages5 = validate(parameterDefinition, 'de') | ||
console.log(messages5) // [ 'only allow a-z, A-Z, 0-9', 'need include demo', 'length should be greater than 5' ] | ||
// translate to zh | ||
const messages6 = validate(parameterInfo, 'de', 'zh') | ||
const messages6 = validate(parameterDefinition, 'de', 'zh') | ||
console.log(messages6) // [ '只允许输入字母和数字', '需要包含demo', '长度应大于5' ] | ||
// translate to zh-TW | ||
const messages7 = validate(parameterInfo, 'de', 'zh-TW') | ||
const messages7 = validate(parameterDefinition, 'de', 'zh-TW') | ||
console.log(messages7) // [ '只允許輸入字母和數字', '需要包含demo', '長度應大於5' ] | ||
@@ -117,3 +192,3 @@ ``` | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'number', | ||
@@ -125,3 +200,3 @@ required: true, | ||
code: 'GREATER_THAN_5', | ||
expression: value => { | ||
function: value => { | ||
return value > 5 | ||
@@ -142,20 +217,88 @@ }, | ||
const messages1 = validate(parameterInfo, 6) | ||
const messages1 = validate(parameterDefinition, 6) | ||
console.log(messages1) // [] | ||
const messages2 = validate(parameterInfo, 5) | ||
const messages2 = validate(parameterDefinition, 5) | ||
console.log(messages2) // [ 'value should be greater than 5' ] | ||
const messages3 = validate(parameterInfo, 4) | ||
const messages3 = validate(parameterDefinition, 4) | ||
console.log(messages3) // [ 'value should be greater than 5' ] | ||
// translate to zh | ||
const messages4 = validate(parameterInfo, 4, 'zh') | ||
const messages4 = validate(parameterDefinition, 4, 'zh') | ||
console.log(messages4) // [ '值应大于5' ] | ||
// translate to zh-TW | ||
const messages5 = validate(parameterInfo, 4, 'zh-TW') | ||
const messages5 = validate(parameterDefinition, 4, 'zh-TW') | ||
console.log(messages5) // [ '值應大於5' ] | ||
``` | ||
### Validate Array Usage | ||
```js | ||
import { validate } from '@sumor/validator' | ||
const parameterDefinition = { | ||
type: 'array', | ||
required: true, | ||
length: 3, | ||
rule: [ | ||
// need greater than 5 | ||
{ | ||
code: 'GREATER_THAN_5', | ||
function: value => { | ||
return value > 5 | ||
}, | ||
message: 'value should be greater than 5' | ||
}, | ||
// should be a number | ||
{ | ||
code: 'SHOULD_BE_NUMBER', | ||
expression: '^[0-9]*$', | ||
message: 'should be a number' | ||
} | ||
] | ||
} | ||
const messages1 = validate(parameterDefinition, [6, 7, 8]) | ||
console.log(messages1) // [] | ||
const messages2 = validate(parameterDefinition, [6, 7, '8']) | ||
console.log(messages2) // [ 'should be a number' ] | ||
const messages3 = validate(parameterDefinition, [6, 7, 4]) | ||
console.log(messages3) // [ 'value should be greater than 5' ] | ||
const messages4 = validate(parameterDefinition, [6, 7, 4, 5]) | ||
console.log(messages4) // [ 'Length must be less than or equal to 3 items', 'value should be greater than 5' ] | ||
``` | ||
### Validate File Usage | ||
```js | ||
import { validate } from '@sumor/validator' | ||
const parameterDefinition = { | ||
type: 'file', | ||
required: true, | ||
length: 3, | ||
rule: [ | ||
// need greater than 5 | ||
{ | ||
code: 'GREATER_THAN_5', | ||
function: value => { | ||
return value.size > 5 | ||
}, | ||
message: 'size should be greater than 5' | ||
} | ||
] | ||
} | ||
const messages1 = validate(parameterDefinition, { size: 6 }) | ||
console.log(messages1) // [] | ||
const messages2 = validate(parameterDefinition, { size: 4 }) | ||
console.log(messages2) // [ 'size should be greater than 5' ] | ||
``` | ||
### Format String Usage | ||
@@ -170,3 +313,3 @@ | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'string', | ||
@@ -176,3 +319,3 @@ trim: true // default is true for string type | ||
const value = format(parameterInfo, ' demo ') | ||
const value = format(parameterDefinition, ' demo ') | ||
console.log(value) // will print "demo", useless space will be removed | ||
@@ -188,3 +331,3 @@ ``` | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'string', | ||
@@ -194,3 +337,3 @@ lowercase: true | ||
const value = format(parameterInfo, 'Demo') | ||
const value = format(parameterDefinition, 'Demo') | ||
console.log(value) // will print "demo", all characters will be converted to lowercase | ||
@@ -206,3 +349,3 @@ ``` | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'string', | ||
@@ -212,3 +355,3 @@ uppercase: true | ||
const value = format(parameterInfo, 'Demo') | ||
const value = format(parameterDefinition, 'Demo') | ||
console.log(value) // will print "DEMO", all characters will be converted to uppercase | ||
@@ -222,3 +365,3 @@ ``` | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
type: 'number', | ||
@@ -228,6 +371,6 @@ decimal: 2 | ||
const value1 = format(parameterInfo, 1.234) | ||
const value1 = format(parameterDefinition, 1.234) | ||
console.log(value1) // will print 1.23, only keep 2 decimal | ||
const value2 = format(parameterInfo, '1.234') | ||
const value2 = format(parameterDefinition, '1.234') | ||
console.log(value2) // will convert to number 1.23, only keep 2 decimal | ||
@@ -244,3 +387,3 @@ ``` | ||
const parameterInfo = { | ||
const parameterDefinition = { | ||
error: true, | ||
@@ -261,4 +404,4 @@ type: 'string', | ||
const messages = validate(parameterInfo, 'demo123456') | ||
console.log(messages) | ||
const messages = validate(parameterDefinition, 'demo123456') | ||
console.log(messages) | ||
/* | ||
@@ -271,2 +414,2 @@ SumorError | ||
*/ | ||
``` | ||
``` |
@@ -7,3 +7,5 @@ // languages: en, zh, es, fr, de, ja, ko, ru, pt, ar | ||
SUMOR_INVALID_NUMBER: 'Invalid number {value}', | ||
SUMOR_NUMBER_LENGTH: 'Length must be less than or equal to {length} digits' | ||
SUMOR_NUMBER_LENGTH: 'Length must be less than or equal to {length} digits', | ||
SUMOR_ARRAY_LENGTH: 'Length must be less than or equal to {length} items', | ||
SUMOR_FILE_LENGTH: 'Length must be less than or equal to {length} files' | ||
}, | ||
@@ -14,3 +16,5 @@ en: { | ||
SUMOR_INVALID_NUMBER: 'Invalid number {value}', | ||
SUMOR_NUMBER_LENGTH: 'Length must be less than or equal to {length} digits' | ||
SUMOR_NUMBER_LENGTH: 'Length must be less than or equal to {length} digits', | ||
SUMOR_ARRAY_LENGTH: 'Length must be less than or equal to {length} items', | ||
SUMOR_FILE_LENGTH: 'Length must be less than or equal to {length} files' | ||
}, | ||
@@ -21,3 +25,5 @@ zh: { | ||
SUMOR_INVALID_NUMBER: '无效数字 {value}', | ||
SUMOR_NUMBER_LENGTH: '长度必须小于等于 {length} 位数' | ||
SUMOR_NUMBER_LENGTH: '长度必须小于等于 {length} 位数', | ||
SUMOR_ARRAY_LENGTH: '长度必须小于等于 {length} 项', | ||
SUMOR_FILE_LENGTH: '长度必须小于等于 {length} 个文件' | ||
}, | ||
@@ -28,3 +34,5 @@ es: { | ||
SUMOR_INVALID_NUMBER: 'Número inválido {value}', | ||
SUMOR_NUMBER_LENGTH: 'La longitud debe ser menor o igual a {length} dígitos' | ||
SUMOR_NUMBER_LENGTH: 'La longitud debe ser menor o igual a {length} dígitos', | ||
SUMOR_ARRAY_LENGTH: 'La longitud debe ser menor o igual a {length} elementos', | ||
SUMOR_FILE_LENGTH: 'La longitud debe ser menor o igual a {length} archivos' | ||
}, | ||
@@ -35,3 +43,5 @@ fr: { | ||
SUMOR_INVALID_NUMBER: 'Nombre invalide {value}', | ||
SUMOR_NUMBER_LENGTH: 'La longueur doit être inférieure ou égale à {length} chiffres' | ||
SUMOR_NUMBER_LENGTH: 'La longueur doit être inférieure ou égale à {length} chiffres', | ||
SUMOR_ARRAY_LENGTH: 'La longueur doit être inférieure ou égale à {length} éléments', | ||
SUMOR_FILE_LENGTH: 'La longueur doit être inférieure ou égale à {length} fichiers' | ||
}, | ||
@@ -42,3 +52,5 @@ de: { | ||
SUMOR_INVALID_NUMBER: 'Ungültige Nummer {value}', | ||
SUMOR_NUMBER_LENGTH: 'Länge muss kleiner oder gleich {length} Ziffern sein' | ||
SUMOR_NUMBER_LENGTH: 'Länge muss kleiner oder gleich {length} Ziffern sein', | ||
SUMOR_ARRAY_LENGTH: 'Länge muss kleiner oder gleich {length} Elemente sein', | ||
SUMOR_FILE_LENGTH: 'Länge muss kleiner oder gleich {length} Dateien sein' | ||
}, | ||
@@ -49,3 +61,5 @@ ja: { | ||
SUMOR_INVALID_NUMBER: '無効な番号 {value}', | ||
SUMOR_NUMBER_LENGTH: '長さは {length} 桁以下である必要があります' | ||
SUMOR_NUMBER_LENGTH: '長さは {length} 桁以下である必要があります', | ||
SUMOR_ARRAY_LENGTH: '長さは {length} 項目以下である必要があります', | ||
SUMOR_FILE_LENGTH: '長さは {length} ファイル以下である必要があります' | ||
}, | ||
@@ -56,3 +70,5 @@ ko: { | ||
SUMOR_INVALID_NUMBER: '잘못된 번호 {value}', | ||
SUMOR_NUMBER_LENGTH: '길이는 {length} 자 이하여야 합니다' | ||
SUMOR_NUMBER_LENGTH: '길이는 {length} 자 이하여야 합니다', | ||
SUMOR_ARRAY_LENGTH: '길이는 {length} 항목 이하여야 합니다', | ||
SUMOR_FILE_LENGTH: '길이는 {length} 파일 이하여야 합니다' | ||
}, | ||
@@ -63,3 +79,5 @@ ru: { | ||
SUMOR_INVALID_NUMBER: 'Неверное число {value}', | ||
SUMOR_NUMBER_LENGTH: 'Длина должна быть не более {length} цифр' | ||
SUMOR_NUMBER_LENGTH: 'Длина должна быть не более {length} цифр', | ||
SUMOR_ARRAY_LENGTH: 'Длина должна быть не более {length} элементов', | ||
SUMOR_FILE_LENGTH: 'Длина должна быть не более {length} файлов' | ||
}, | ||
@@ -70,3 +88,5 @@ pt: { | ||
SUMOR_INVALID_NUMBER: 'Número inválido {value}', | ||
SUMOR_NUMBER_LENGTH: 'O comprimento deve ser menor ou igual a {length} dígitos' | ||
SUMOR_NUMBER_LENGTH: 'O comprimento deve ser menor ou igual a {length} dígitos', | ||
SUMOR_ARRAY_LENGTH: 'O comprimento deve ser menor ou igual a {length} itens', | ||
SUMOR_FILE_LENGTH: 'O comprimento deve ser menor ou igual a {length} arquivos' | ||
}, | ||
@@ -77,4 +97,6 @@ ar: { | ||
SUMOR_INVALID_NUMBER: 'رقم غير صالح {value}', | ||
SUMOR_NUMBER_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} أرقام' | ||
SUMOR_NUMBER_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} أرقام', | ||
SUMOR_ARRAY_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} عناصر', | ||
SUMOR_FILE_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} ملفات' | ||
} | ||
} |
@@ -6,2 +6,7 @@ import parseInfo from './parseInfo.js' | ||
import numberValidate from './number/validate.js' | ||
import arrayFormat from './array/format.js' | ||
import arrayValidate from './array/validate.js' | ||
import fileFormat from './file/format.js' | ||
import fileValidate from './file/validate.js' | ||
import getI18n from './i18n/index.js' | ||
@@ -23,2 +28,8 @@ import getError from './error/index.js' | ||
break | ||
case 'array': | ||
formattedValue = arrayFormat(info, value) | ||
break | ||
case 'file': | ||
formattedValue = fileFormat(info, value) | ||
break | ||
} | ||
@@ -51,2 +62,16 @@ | ||
break | ||
case 'array': | ||
formattedValue = arrayFormat(info, value) | ||
messages = arrayValidate(info, formattedValue) | ||
if (formattedValue) { | ||
valueLength = formattedValue.length | ||
} | ||
break | ||
case 'file': | ||
formattedValue = fileFormat(info, value) | ||
messages = fileValidate(info, formattedValue) | ||
if (formattedValue) { | ||
valueLength = formattedValue.length | ||
} | ||
break | ||
} | ||
@@ -56,5 +81,14 @@ | ||
if (rule.function) { | ||
const result = rule.function(formattedValue, info, language) | ||
if (!result) { | ||
messages.push(rule.code) | ||
if (info.type === 'array' || info.type === 'file') { | ||
for (const item of formattedValue) { | ||
const result = rule.function(item, info, language) | ||
if (!result) { | ||
messages.push(rule.code) | ||
} | ||
} | ||
} else { | ||
const result = rule.function(formattedValue, info, language) | ||
if (!result) { | ||
messages.push(rule.code) | ||
} | ||
} | ||
@@ -61,0 +95,0 @@ } |
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
27310
18
429
398