@vitrical/utils
Advanced tools
Comparing version 1.0.2 to 1.0.3
{ | ||
"name": "@vitrical/utils", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Collection of useful functions and typings", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -12,3 +12,3 @@ "use strict"; | ||
exports.__esModule = true; | ||
exports.validateObject = exports.getSeason = exports.capitalize = exports.isValidNAPhoneNumber = exports.formatPhoneNumber = exports.isValidEmail = exports.removeSpaces = exports.removeSpecialCharacters = exports.isApiError = exports.objectHasProperty = void 0; | ||
exports.validateObject = exports.isValidationType = exports.getSeason = exports.capitalize = exports.isValidNAPhoneNumber = exports.formatPhoneNumber = exports.isValidEmail = exports.removeSpaces = exports.removeSpecialCharacters = exports.isApiError = exports.objectHasProperty = void 0; | ||
var objectHasProperty = function (obj, property) { | ||
@@ -78,2 +78,64 @@ return !!obj && typeof obj === 'object' && property in obj; | ||
exports.getSeason = getSeason; | ||
var isValidationType = function (validationType, value) { | ||
switch (validationType) { | ||
case 'array': | ||
if (!Array.isArray(value)) | ||
return false; | ||
break; | ||
case 'array?': | ||
if (typeof value !== 'undefined' && !Array.isArray(value)) | ||
return false; | ||
break; | ||
case 'array[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return !Array.isArray(v); })) | ||
return false; | ||
break; | ||
case 'bigint?': | ||
if (typeof value !== 'undefined' && typeof value !== 'bigint') | ||
return false; | ||
break; | ||
case 'bigint[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return typeof v !== 'bigint'; })) | ||
return false; | ||
break; | ||
case 'boolean?': | ||
if (typeof value !== 'undefined' && typeof value !== 'boolean') | ||
return false; | ||
break; | ||
case 'boolean[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return typeof v !== 'boolean'; })) | ||
return false; | ||
break; | ||
case 'number?': | ||
if (typeof value !== 'undefined' && typeof value !== 'number') | ||
return false; | ||
break; | ||
case 'number[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return typeof v !== 'number'; })) | ||
return false; | ||
break; | ||
case 'object?': | ||
if (typeof value !== 'undefined' && typeof value !== 'object') | ||
return false; | ||
break; | ||
case 'object[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return typeof v !== 'object'; })) | ||
return false; | ||
break; | ||
case 'string?': | ||
if (typeof value !== 'undefined' && typeof value !== 'string') | ||
return false; | ||
break; | ||
case 'string[]': | ||
if (!Array.isArray(value) || value.some(function (v) { return typeof v !== 'string'; })) | ||
return false; | ||
break; | ||
default: | ||
if (typeof value !== validationType) | ||
return false; | ||
break; | ||
} | ||
return true; | ||
}; | ||
exports.isValidationType = isValidationType; | ||
function validateObject(schema, object) { | ||
@@ -114,43 +176,4 @@ var bodyErrors = Object.keys(object) | ||
} | ||
switch (validationType) { | ||
case 'array': | ||
if (!Array.isArray(value)) { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'array?': | ||
if (!Array.isArray(value)) { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'bigint?': | ||
if (typeof value !== 'bigint') { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'boolean?': | ||
if (typeof value !== 'boolean') { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'number?': | ||
if (typeof value !== 'number') { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'object?': | ||
if (typeof value !== 'object') { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
case 'string?': | ||
if (typeof value !== 'string') { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
default: | ||
if (typeof value !== validationType) { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
break; | ||
if (!(0, exports.isValidationType)(validationType, value)) { | ||
return "Expected ".concat(validationType, " but got ").concat(typeof value, " ").concat(value, " from property \"").concat(key, "\""); | ||
} | ||
@@ -157,0 +180,0 @@ if (typeof validationSchema !== 'string') { |
@@ -91,5 +91,18 @@ export const objectHasProperty = ( | ||
| 'object?' | ||
type ValidationArray = | ||
| 'number[]' | ||
| 'string[]' | ||
| 'boolean[]' | ||
| 'object[]' | ||
| 'bigint[]' | ||
| 'undefined[]' | ||
| 'array[]' | ||
type MaxLength = number | ||
type MinLength = number | ||
type SchemaInput = [ValidationLength, MinLength, MaxLength] | ValidationRegular | ValidationLength | ||
type SchemaInput = | ||
| [ValidationLength, MinLength, MaxLength] | ||
| ValidationRegular | ||
| ValidationLength | ||
| ValidationArray | ||
| [ValidationArray, MinLength, MaxLength] | ||
export type ValidateObjectOptions = { | ||
@@ -99,2 +112,53 @@ [key: string]: SchemaInput | ValidateObjectOptions | ||
export const isValidationType = ( | ||
validationType: ValidationLength | ValidationArray | ValidationRegular, | ||
value: unknown | ||
): boolean => { | ||
switch (validationType) { | ||
case 'array': | ||
if (!Array.isArray(value)) return false | ||
break | ||
case 'array?': | ||
if (typeof value !== 'undefined' && !Array.isArray(value)) return false | ||
break | ||
case 'array[]': | ||
if (!Array.isArray(value) || value.some((v) => !Array.isArray(v))) return false | ||
break | ||
case 'bigint?': | ||
if (typeof value !== 'undefined' && typeof value !== 'bigint') return false | ||
break | ||
case 'bigint[]': | ||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'bigint')) return false | ||
break | ||
case 'boolean?': | ||
if (typeof value !== 'undefined' && typeof value !== 'boolean') return false | ||
break | ||
case 'boolean[]': | ||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'boolean')) return false | ||
break | ||
case 'number?': | ||
if (typeof value !== 'undefined' && typeof value !== 'number') return false | ||
break | ||
case 'number[]': | ||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'number')) return false | ||
break | ||
case 'object?': | ||
if (typeof value !== 'undefined' && typeof value !== 'object') return false | ||
break | ||
case 'object[]': | ||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'object')) return false | ||
break | ||
case 'string?': | ||
if (typeof value !== 'undefined' && typeof value !== 'string') return false | ||
break | ||
case 'string[]': | ||
if (!Array.isArray(value) || value.some((v) => typeof v !== 'string')) return false | ||
break | ||
default: | ||
if (typeof value !== validationType) return false | ||
break | ||
} | ||
return true | ||
} | ||
/* Returns a list of errors if the object does not match the schema */ | ||
@@ -150,43 +214,4 @@ export function validateObject( | ||
switch (validationType) { | ||
case 'array': | ||
if (!Array.isArray(value)) { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'array?': | ||
if (!Array.isArray(value)) { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'bigint?': | ||
if (typeof value !== 'bigint') { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'boolean?': | ||
if (typeof value !== 'boolean') { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'number?': | ||
if (typeof value !== 'number') { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'object?': | ||
if (typeof value !== 'object') { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
case 'string?': | ||
if (typeof value !== 'string') { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
default: | ||
if (typeof value !== validationType) { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
break | ||
if (!isValidationType(validationType, value)) { | ||
return `Expected ${validationType} but got ${typeof value} ${value} from property "${key}"` | ||
} | ||
@@ -193,0 +218,0 @@ |
Sorry, the diff of this file is not supported yet
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
44356
817