@braiandev/string-validator
Advanced tools
Comparing version 1.0.1-beta to 1.0.3-beta
@@ -65,2 +65,3 @@ /** | ||
lang?: Lang; | ||
trim?: boolean; | ||
results?: RichResults; | ||
@@ -97,2 +98,3 @@ } | ||
limits?: LimitsOptions; | ||
trim: boolean; | ||
} | ||
@@ -99,0 +101,0 @@ /** Results for num() method */ |
@@ -11,2 +11,4 @@ import { strVal } from "../types/types"; | ||
private mode; | ||
/** Remove white spaces at the beginning and the end of the string */ | ||
private trim; | ||
/** RegExp collection to test strings. */ | ||
@@ -34,4 +36,3 @@ private testRegExp; | ||
* properly. | ||
* The error displayed depends on the error code passed ass an argument, in addition, some extra | ||
* argument can be passed to display dynamic error messages. | ||
* The error displayed depends on the error code passed ass an argument. | ||
*/ | ||
@@ -38,0 +39,0 @@ private throwError; |
@@ -18,2 +18,3 @@ "use strict"; | ||
var failures_1 = __importDefault(require("./failures")); | ||
var errors_1 = __importDefault(require("./errors")); | ||
/** | ||
@@ -36,2 +37,4 @@ * Core validator class, contains all the testing methods for strings and numbers | ||
this.mode = "easy"; | ||
/** Remove white spaces at the beginning and the end of the string */ | ||
this.trim = false; | ||
/** Collection of methods to validate different number types */ | ||
@@ -50,2 +53,4 @@ this.numberTests = { | ||
limits: true, | ||
lang: true, | ||
trim: true | ||
}; | ||
@@ -60,3 +65,3 @@ this.removeUnwantedResults = function (results) { | ||
if (settings && settings.mode && (settings.mode !== "easy" && settings.mode !== "rich")) | ||
this.throwError("300", settings.mode); | ||
this.throwError("300"); | ||
// Set props | ||
@@ -69,2 +74,4 @@ if (settings && settings.mode) | ||
this.lang = settings.lang; | ||
if (settings && settings.trim) | ||
this.trim = settings.trim; | ||
// Set string regex tests values | ||
@@ -98,26 +105,6 @@ this.testRegExp = require("./testRegExp")[this.lang]; | ||
* properly. | ||
* The error displayed depends on the error code passed ass an argument, in addition, some extra | ||
* argument can be passed to display dynamic error messages. | ||
* The error displayed depends on the error code passed ass an argument. | ||
*/ | ||
Validator.prototype.throwError = function (code, arg) { | ||
switch (code) { | ||
// Datatypes errors | ||
case "000": throw new Error("Expected a string datatype to validate, instead received: '" + arg + "'"); | ||
case "001": throw new Error("Expected a number datatype to validate, instead received: '" + arg + "'"); | ||
case "002": throw new Error("Unexpected datatype on length options, expected all values to be a number."); | ||
case "003": throw new Error("Unexpected datatype on range options, expected all values to be a number."); | ||
case "004": throw new Error("Wrong regular expression value on argument, expected a valid string/RegExp pattern."); | ||
case "005": throw new Error("Wrong key value on argument, expected a valid object-key string."); | ||
// Range errors | ||
case "100": throw new Error("Wrong length options configuration, 'min' value must be minor than or equal to 'max' value."); | ||
case "101": throw new Error("Wrong range options configuration, 'min' value must be minor than or equal to 'max' value."); | ||
// Test types errors | ||
case "200": throw new Error("The test value you are trying to create has an invalid string/RegExp format."); | ||
case "201": throw new Error("Test type '" + arg + "' is not a valid string test key."); | ||
case "202": throw new Error("Test type '" + arg + "' is not a valid number test key."); | ||
case "203": throw new Error("Test type key argument is not a valid object-key string."); | ||
// Settings errors | ||
case "300": throw new Error("Wrong instance, '" + arg + "' is not a valid mode."); | ||
default: throw new Error("Error code '" + code + "' doesn't exist."); // Debug purposes only | ||
} | ||
Validator.prototype.throwError = function (code) { | ||
throw new Error(errors_1.default[code]); | ||
}; | ||
@@ -249,3 +236,3 @@ /** | ||
test = false; | ||
this.throwError("201", type); | ||
this.throwError("201"); | ||
} | ||
@@ -283,3 +270,3 @@ /** | ||
test = false; | ||
this.throwError("202", type); | ||
this.throwError("202"); | ||
} | ||
@@ -318,3 +305,6 @@ /** | ||
if (typeof subject !== "string") | ||
this.throwError("000", typeof subject); | ||
this.throwError("000"); | ||
var testSubject = subject; | ||
if (this.trim) | ||
testSubject = subject.trim(); | ||
/** | ||
@@ -328,3 +318,3 @@ * -------------- | ||
if (limits) { | ||
if (!this.testLength(subject.length, limits)) | ||
if (!this.testLength(testSubject.length, limits)) | ||
return false; | ||
@@ -334,3 +324,3 @@ } | ||
if (test) { | ||
return this.testString(subject, test.toLowerCase()); | ||
return this.testString(testSubject, test.toLowerCase()); | ||
} | ||
@@ -348,5 +338,6 @@ // Default return | ||
var results = {}; | ||
results.subject = subject; | ||
results.length = subject.length; | ||
results.subject = testSubject; | ||
results.length = testSubject.length; | ||
results.lang = this.lang; | ||
results.trim = this.trim; | ||
if (limits) | ||
@@ -358,3 +349,3 @@ results.limits = limits; | ||
if (limits) { | ||
results = __assign(__assign({}, results), this.testLength(subject.length, limits)); | ||
results = __assign(__assign({}, results), this.testLength(testSubject.length, limits)); | ||
if (!results.result) | ||
@@ -365,3 +356,3 @@ return this.removeUnwantedResults(results); | ||
if (test) { | ||
results = __assign(__assign({}, results), this.testString(subject, test.toLowerCase())); | ||
results = __assign(__assign({}, results), this.testString(testSubject, test.toLowerCase())); | ||
return this.removeUnwantedResults(results); | ||
@@ -387,3 +378,3 @@ } | ||
if (typeof subject !== "number") | ||
this.throwError("001", typeof subject); | ||
this.throwError("001"); | ||
/** | ||
@@ -390,0 +381,0 @@ * -------------- |
@@ -65,2 +65,3 @@ /** | ||
lang?: Lang; | ||
trim?: boolean; | ||
results?: RichResults; | ||
@@ -97,2 +98,3 @@ } | ||
limits?: LimitsOptions; | ||
trim: boolean; | ||
} | ||
@@ -99,0 +101,0 @@ /** Results for num() method */ |
@@ -11,2 +11,4 @@ import { strVal } from "../types/types"; | ||
private mode; | ||
/** Remove white spaces at the beginning and the end of the string */ | ||
private trim; | ||
/** RegExp collection to test strings. */ | ||
@@ -34,4 +36,3 @@ private testRegExp; | ||
* properly. | ||
* The error displayed depends on the error code passed ass an argument, in addition, some extra | ||
* argument can be passed to display dynamic error messages. | ||
* The error displayed depends on the error code passed ass an argument. | ||
*/ | ||
@@ -38,0 +39,0 @@ private throwError; |
@@ -13,2 +13,3 @@ var __assign = (this && this.__assign) || function () { | ||
import failures from "./failures"; | ||
import errors from "./errors"; | ||
/** | ||
@@ -31,2 +32,4 @@ * Core validator class, contains all the testing methods for strings and numbers | ||
this.mode = "easy"; | ||
/** Remove white spaces at the beginning and the end of the string */ | ||
this.trim = false; | ||
/** Collection of methods to validate different number types */ | ||
@@ -45,2 +48,4 @@ this.numberTests = { | ||
limits: true, | ||
lang: true, | ||
trim: true | ||
}; | ||
@@ -55,3 +60,3 @@ this.removeUnwantedResults = function (results) { | ||
if (settings && settings.mode && (settings.mode !== "easy" && settings.mode !== "rich")) | ||
this.throwError("300", settings.mode); | ||
this.throwError("300"); | ||
// Set props | ||
@@ -64,2 +69,4 @@ if (settings && settings.mode) | ||
this.lang = settings.lang; | ||
if (settings && settings.trim) | ||
this.trim = settings.trim; | ||
// Set string regex tests values | ||
@@ -93,26 +100,6 @@ this.testRegExp = require("./testRegExp")[this.lang]; | ||
* properly. | ||
* The error displayed depends on the error code passed ass an argument, in addition, some extra | ||
* argument can be passed to display dynamic error messages. | ||
* The error displayed depends on the error code passed ass an argument. | ||
*/ | ||
Validator.prototype.throwError = function (code, arg) { | ||
switch (code) { | ||
// Datatypes errors | ||
case "000": throw new Error("Expected a string datatype to validate, instead received: '" + arg + "'"); | ||
case "001": throw new Error("Expected a number datatype to validate, instead received: '" + arg + "'"); | ||
case "002": throw new Error("Unexpected datatype on length options, expected all values to be a number."); | ||
case "003": throw new Error("Unexpected datatype on range options, expected all values to be a number."); | ||
case "004": throw new Error("Wrong regular expression value on argument, expected a valid string/RegExp pattern."); | ||
case "005": throw new Error("Wrong key value on argument, expected a valid object-key string."); | ||
// Range errors | ||
case "100": throw new Error("Wrong length options configuration, 'min' value must be minor than or equal to 'max' value."); | ||
case "101": throw new Error("Wrong range options configuration, 'min' value must be minor than or equal to 'max' value."); | ||
// Test types errors | ||
case "200": throw new Error("The test value you are trying to create has an invalid string/RegExp format."); | ||
case "201": throw new Error("Test type '" + arg + "' is not a valid string test key."); | ||
case "202": throw new Error("Test type '" + arg + "' is not a valid number test key."); | ||
case "203": throw new Error("Test type key argument is not a valid object-key string."); | ||
// Settings errors | ||
case "300": throw new Error("Wrong instance, '" + arg + "' is not a valid mode."); | ||
default: throw new Error("Error code '" + code + "' doesn't exist."); // Debug purposes only | ||
} | ||
Validator.prototype.throwError = function (code) { | ||
throw new Error(errors[code]); | ||
}; | ||
@@ -244,3 +231,3 @@ /** | ||
test = false; | ||
this.throwError("201", type); | ||
this.throwError("201"); | ||
} | ||
@@ -278,3 +265,3 @@ /** | ||
test = false; | ||
this.throwError("202", type); | ||
this.throwError("202"); | ||
} | ||
@@ -313,3 +300,6 @@ /** | ||
if (typeof subject !== "string") | ||
this.throwError("000", typeof subject); | ||
this.throwError("000"); | ||
var testSubject = subject; | ||
if (this.trim) | ||
testSubject = subject.trim(); | ||
/** | ||
@@ -323,3 +313,3 @@ * -------------- | ||
if (limits) { | ||
if (!this.testLength(subject.length, limits)) | ||
if (!this.testLength(testSubject.length, limits)) | ||
return false; | ||
@@ -329,3 +319,3 @@ } | ||
if (test) { | ||
return this.testString(subject, test.toLowerCase()); | ||
return this.testString(testSubject, test.toLowerCase()); | ||
} | ||
@@ -343,5 +333,6 @@ // Default return | ||
var results = {}; | ||
results.subject = subject; | ||
results.length = subject.length; | ||
results.subject = testSubject; | ||
results.length = testSubject.length; | ||
results.lang = this.lang; | ||
results.trim = this.trim; | ||
if (limits) | ||
@@ -353,3 +344,3 @@ results.limits = limits; | ||
if (limits) { | ||
results = __assign(__assign({}, results), this.testLength(subject.length, limits)); | ||
results = __assign(__assign({}, results), this.testLength(testSubject.length, limits)); | ||
if (!results.result) | ||
@@ -360,3 +351,3 @@ return this.removeUnwantedResults(results); | ||
if (test) { | ||
results = __assign(__assign({}, results), this.testString(subject, test.toLowerCase())); | ||
results = __assign(__assign({}, results), this.testString(testSubject, test.toLowerCase())); | ||
return this.removeUnwantedResults(results); | ||
@@ -382,3 +373,3 @@ } | ||
if (typeof subject !== "number") | ||
this.throwError("001", typeof subject); | ||
this.throwError("001"); | ||
/** | ||
@@ -385,0 +376,0 @@ * -------------- |
{ | ||
"name": "@braiandev/string-validator", | ||
"version": "1.0.1-beta", | ||
"version": "1.0.3-beta", | ||
"description": "This little library written in Typescript was made to serve as a faster and reliable easy-to-use tool for strings validations that come *specially* from UI forms, but can be used to test any string type no matter the source.", | ||
@@ -18,3 +18,4 @@ "author": "Braian Rosas", | ||
"scripts": { | ||
"test": "jest", | ||
"test": "jest --watchAll", | ||
"lint": "eslint", | ||
"watch:build:esm": "tsc --watch", | ||
@@ -21,0 +22,0 @@ "watch:build:cjs": "tsc --watch --module commonjs --outDir dist/cjs", |
@@ -133,3 +133,4 @@ # String Validator Library :heavy_check_mark: | ||
length: 16, | ||
test: "password" | ||
test: "password", | ||
trim: false | ||
} */ | ||
@@ -143,3 +144,4 @@ console.log(bad) /* { | ||
failure: "NOSTRMATCH", | ||
description: "String doesn't match with the specified test type." | ||
description: "String doesn't match with the specified test type.", | ||
trim: false | ||
} */ | ||
@@ -214,2 +216,3 @@ ``` | ||
`limits` | `object` | :heavy_check_mark: | :heavy_check_mark: | Limits tested (if defined) | ||
`trim` | `boolean` | :heavy_check_mark: | :heavy_check_mark: | If string was trimmed or not (only when testing strings) | ||
@@ -227,2 +230,3 @@ When a test returns failure, the `failure` and `description` can help you to understand where was the problem with the subject, the possible failure codes and descriptions are below: | ||
`"WRONGSTRLIMITS"` | `"Limits can't be negative values while testing strings."` | ||
`"WRONGLIMITS"` | `"Max. limit value can't be less than min. limit value."` | ||
@@ -235,3 +239,3 @@ | ||
By default the validator has three properties that can be managed in `settings` with an object with the following keys: `mode`, `lang`, `results`. | ||
By default the validator has three properties that can be managed in `settings` with an object with the following keys: `mode`, `lang`, `results` and `trim`. | ||
@@ -241,2 +245,3 @@ * `mode` is how the results of the validations should return. | ||
* `results`, when `mode` is set to `"rich"` you can choose wich data won't be returned from the validations. | ||
* `trim` defines if a tested string must be trimmed, if true, string will be trimmed before any limit or type test. | ||
@@ -248,2 +253,3 @@ Example: | ||
lang: "es", | ||
trim: false, | ||
results: { | ||
@@ -257,2 +263,3 @@ failure: true, | ||
lang: true | ||
trim: true, | ||
} | ||
@@ -266,2 +273,3 @@ }); | ||
`lang` | `string` | `"en"\|"es"\|"br"\|"fr"\|"de"` | ||
`trim` | `boolean` | `true\|false` | ||
`results` | `object` | Example: `{string: false, length: false}` | ||
@@ -342,2 +350,3 @@ | ||
`limits` | `boolean` | `true` | Return the limits if setted, i.e: `{min: 0, max: 100}` | ||
`trim` | `boolean` | `true` | Return if string was trimmed or not | ||
@@ -344,0 +353,0 @@ ## Add custom tests |
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
27
1416
362
78794