@sumor/validator
Advanced tools
Comparing version 1.1.0 to 1.2.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.1.0", | ||
"version": "1.2.0", | ||
"license": "MIT", | ||
@@ -50,4 +50,4 @@ "repository": "sumor-cloud/validator", | ||
"prepare": "husky", | ||
"check": "eslint --fix . && prettier --write . && npm audit fix --force && npm run coverage" | ||
"check": "npm run autofix && npm run coverage" | ||
} | ||
} |
@@ -74,3 +74,3 @@ # validator | ||
- [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] `decimal` decimal for the number, default is null, 0 means no decimal | ||
- [optional] `i18n` i18n for the error message, default is empty object, please refer to the following section for the detail | ||
@@ -77,0 +77,0 @@ - [optional] `error` output error or not, default is false |
@@ -9,3 +9,4 @@ // languages: en, zh, es, fr, de, ja, ko, ru, pt, ar | ||
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' | ||
SUMOR_FILE_LENGTH: 'Length must be less than or equal to {length} files', | ||
SUMOR_ITEM_RULE_FAILED: 'Item {index} validation failed' | ||
}, | ||
@@ -18,3 +19,4 @@ en: { | ||
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' | ||
SUMOR_FILE_LENGTH: 'Length must be less than or equal to {length} files', | ||
SUMOR_ITEM_RULE_FAILED: 'Item {index} validation failed' | ||
}, | ||
@@ -27,3 +29,4 @@ zh: { | ||
SUMOR_ARRAY_LENGTH: '长度必须小于等于 {length} 项', | ||
SUMOR_FILE_LENGTH: '长度必须小于等于 {length} 个文件' | ||
SUMOR_FILE_LENGTH: '长度必须小于等于 {length} 个文件', | ||
SUMOR_ITEM_RULE_FAILED: '第 {index} 项验证失败' | ||
}, | ||
@@ -36,3 +39,4 @@ es: { | ||
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' | ||
SUMOR_FILE_LENGTH: 'La longitud debe ser menor o igual a {length} archivos', | ||
SUMOR_ITEM_RULE_FAILED: 'La validación del elemento {index} falló' | ||
}, | ||
@@ -45,3 +49,4 @@ fr: { | ||
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' | ||
SUMOR_FILE_LENGTH: 'La longueur doit être inférieure ou égale à {length} fichiers', | ||
SUMOR_ITEM_RULE_FAILED: "La validation de l'élément {index} a échoué" | ||
}, | ||
@@ -54,3 +59,4 @@ de: { | ||
SUMOR_ARRAY_LENGTH: 'Länge muss kleiner oder gleich {length} Elemente sein', | ||
SUMOR_FILE_LENGTH: 'Länge muss kleiner oder gleich {length} Dateien sein' | ||
SUMOR_FILE_LENGTH: 'Länge muss kleiner oder gleich {length} Dateien sein', | ||
SUMOR_ITEM_RULE_FAILED: 'Die Validierung des Elements {index} ist fehlgesch lagen' | ||
}, | ||
@@ -63,3 +69,4 @@ ja: { | ||
SUMOR_ARRAY_LENGTH: '長さは {length} 項目以下である必要があります', | ||
SUMOR_FILE_LENGTH: '長さは {length} ファイル以下である必要があります' | ||
SUMOR_FILE_LENGTH: '長さは {length} ファイル以下である必要があります', | ||
SUMOR_ITEM_RULE_FAILED: 'アイテム {index} の検証に失敗しました' | ||
}, | ||
@@ -72,3 +79,4 @@ ko: { | ||
SUMOR_ARRAY_LENGTH: '길이는 {length} 항목 이하여야 합니다', | ||
SUMOR_FILE_LENGTH: '길이는 {length} 파일 이하여야 합니다' | ||
SUMOR_FILE_LENGTH: '길이는 {length} 파일 이하여야 합니다', | ||
SUMOR_ITEM_RULE_FAILED: '항목 {index} 유효성 검사 실패' | ||
}, | ||
@@ -81,3 +89,4 @@ ru: { | ||
SUMOR_ARRAY_LENGTH: 'Длина должна быть не более {length} элементов', | ||
SUMOR_FILE_LENGTH: 'Длина должна быть не более {length} файлов' | ||
SUMOR_FILE_LENGTH: 'Длина должна быть не более {length} файлов', | ||
SUMOR_ITEM_RULE_FAILED: 'Проверка элемента {index} не удалась' | ||
}, | ||
@@ -90,3 +99,4 @@ pt: { | ||
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' | ||
SUMOR_FILE_LENGTH: 'O comprimento deve ser menor ou igual a {length} arquivos', | ||
SUMOR_ITEM_RULE_FAILED: 'A validação do item {index} falhou' | ||
}, | ||
@@ -99,4 +109,5 @@ ar: { | ||
SUMOR_ARRAY_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} عناصر', | ||
SUMOR_FILE_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} ملفات' | ||
SUMOR_FILE_LENGTH: 'يجب أن يكون الطول أقل من أو يساوي {length} ملفات', | ||
SUMOR_ITEM_RULE_FAILED: 'فشل التحقق من صحة العنصر {index}' | ||
} | ||
} |
121
src/index.js
@@ -1,122 +0,5 @@ | ||
import parseInfo from './parseInfo.js' | ||
import stringFormat from './string/format.js' | ||
import stringValidate from './string/validate.js' | ||
import numberFormat from './number/format.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 validate from './validate/index.js' | ||
import format from './format/index.js' | ||
import getI18n from './i18n/index.js' | ||
import getError from './error/index.js' | ||
const format = (info, value) => { | ||
info = parseInfo(info) | ||
info.error = !!info.error // if error is true, will return error object | ||
let formattedValue = value | ||
switch (info.type) { | ||
case 'string': | ||
formattedValue = stringFormat(info, value) | ||
break | ||
case 'number': | ||
formattedValue = numberFormat(info, value) | ||
break | ||
case 'array': | ||
formattedValue = arrayFormat(info, value) | ||
break | ||
case 'file': | ||
formattedValue = fileFormat(info, value) | ||
break | ||
} | ||
return formattedValue | ||
} | ||
const validate = (info, value, language = 'en-US') => { | ||
info = parseInfo(info) | ||
let formattedValue = value | ||
let valueLength = 0 | ||
let messages = [] | ||
switch (info.type) { | ||
case 'string': | ||
formattedValue = stringFormat(info, value) | ||
messages = stringValidate(info, formattedValue) | ||
if (formattedValue) { | ||
valueLength = formattedValue.length | ||
} | ||
break | ||
case 'number': | ||
formattedValue = numberFormat(info, value) | ||
messages = numberValidate(info, formattedValue) | ||
if (!isNaN(formattedValue)) { | ||
valueLength = parseInt(formattedValue, 10).toString().length | ||
} | ||
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 | ||
} | ||
for (const rule of info.rule) { | ||
if (rule.function) { | ||
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) | ||
} | ||
} | ||
} | ||
} | ||
if (info.error) { | ||
const ValidationError = getError(language, info) | ||
messages = messages.map(message => { | ||
return new ValidationError(message, { | ||
value, | ||
currentLength: valueLength, | ||
...info | ||
}) | ||
}) | ||
} else { | ||
const i18n = getI18n(language, info) | ||
messages = messages.map(message => { | ||
return { | ||
code: message, | ||
message: i18n(message, { | ||
value, | ||
currentLength: valueLength, | ||
...info | ||
}) | ||
} | ||
}) | ||
} | ||
return messages | ||
} | ||
export { validate, format } | ||
export default { | ||
@@ -123,0 +6,0 @@ validate, |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
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
28190
20
435
1