@local901/validator 0.0.5 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/errors/ValidationError.d.ts +4 -1
- package/errors/ValidationError.js.map +1 -1
- package/index.d.ts +7 -3
- package/index.js +5 -1
- package/index.js.map +1 -1
- package/package.json +1 -1
- package/types/number/IntValidator.js +2 -2
- package/types/number/IntValidator.js.map +1 -1
- package/types/number/NumberValidator.js +2 -2
- package/types/number/NumberValidator.js.map +1 -1
- package/types/object/ArrayValidator.d.ts +8 -1
- package/types/object/ArrayValidator.js +16 -6
- package/types/object/ArrayValidator.js.map +1 -1
- package/types/object/ObjectValidator.js +1 -1
- package/types/object/ObjectValidator.js.map +1 -1
- package/types/object/TupleValidator.d.ts +9 -0
- package/types/object/TupleValidator.js +40 -0
- package/types/object/TupleValidator.js.map +1 -0
- package/types/string/StringValidator.d.ts +2 -2
- package/types/string/StringValidator.js +4 -4
- package/types/string/StringValidator.js.map +1 -1
- package/types/util/InstanceofValidator.d.ts +24 -0
- package/types/util/InstanceofValidator.js +44 -0
- package/types/util/InstanceofValidator.js.map +1 -0
- package/types/util/OptionalValidator.d.ts +5 -1
- package/types/util/OptionalValidator.js +8 -2
- package/types/util/OptionalValidator.js.map +1 -1
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ErrorValue } from "./ErrorType";
|
|
2
|
-
|
|
2
|
+
type TupleError<T extends ReadonlyArray<unknown>> = T extends [infer ITEM, ...infer ARR] ? [ValidationError<ITEM> | null, ...ARR extends never[] ? [] : TupleError<ARR>] : never;
|
|
3
|
+
type ArrayError<T extends ReadonlyArray<unknown>> = T extends [infer _ITEM, ...infer _ARR] ? TupleError<T> : T extends ReadonlyArray<infer ARR> ? Record<number, ValidationError<ARR> | null> : never;
|
|
4
|
+
export type ErrorFields<T> = (T extends object ? T extends readonly unknown[] ? ArrayError<T> : {
|
|
3
5
|
[K in keyof T]: ValidationError<T[K]> | undefined;
|
|
4
6
|
} : never) | Record<number, ValidationError<T>>;
|
|
5
7
|
export declare class ValidationError<T extends unknown = unknown> extends Error {
|
|
@@ -8,3 +10,4 @@ export declare class ValidationError<T extends unknown = unknown> extends Error
|
|
|
8
10
|
readonly fields?: ErrorFields<T>;
|
|
9
11
|
constructor(type: string, error: ErrorValue, message: string, fields?: ErrorFields<T>);
|
|
10
12
|
}
|
|
13
|
+
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ValidationError.js","sourceRoot":"","sources":["../../src/errors/ValidationError.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"ValidationError.js","sourceRoot":"","sources":["../../src/errors/ValidationError.ts"],"names":[],"mappings":";;;AAkBA,MAAa,eAEX,SAAQ,KAAK;IACK,IAAI,CAAS;IACb,KAAK,CAAa;IAClB,MAAM,CAAiB;IAEvC,YAAmB,IAAY,EAAE,KAAiB,EAAE,OAAe,EAAE,MAAuB;QACxF,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QACjB,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC;IACzB,CAAC;CACJ;AAbD,0CAaC"}
|
package/index.d.ts
CHANGED
|
@@ -7,8 +7,10 @@ import { stringValidator, type StringValidationOptions } from "./types/string/St
|
|
|
7
7
|
import { Validator } from "./Validator";
|
|
8
8
|
import { AnyValidator } from "./types/util/AnyValidator";
|
|
9
9
|
import { OrValidator, type OrValidators } from "./types/util/OrValidator";
|
|
10
|
-
import { ArrayValidator } from "./types/object/ArrayValidator";
|
|
10
|
+
import { ArrayValidator, type ArrayValidatorOptions } from "./types/object/ArrayValidator";
|
|
11
11
|
import { EnumValidator, type EnumLike } from "./types/object/EnumValidator";
|
|
12
|
+
import { TupleValidator, type TupleValidators } from "./types/object/TupleValidator";
|
|
13
|
+
import { InstanceofValidator, type ConstructorType, type InstanceofValidationOptions } from "./types/util/InstanceofValidator";
|
|
12
14
|
export { Validator } from "./Validator";
|
|
13
15
|
export { ErrorType, type ErrorValue } from "./errors/ErrorType";
|
|
14
16
|
export { ValidationError, type ErrorFields } from "./errors/ValidationError";
|
|
@@ -16,11 +18,13 @@ export declare class v {
|
|
|
16
18
|
static boolean: () => BooleanValidator;
|
|
17
19
|
static int: (options?: IntValidationOptions) => IntValidator;
|
|
18
20
|
static number: (options?: NumberValidationOptions) => NumberValidator;
|
|
19
|
-
static array: <T>(item: Validator<T
|
|
21
|
+
static array: <T>(item: Validator<T>, options?: ArrayValidatorOptions) => ArrayValidator<T>;
|
|
20
22
|
static enum: <T extends EnumLike>(enumInstance: T) => EnumValidator<T>;
|
|
21
23
|
static object: <T extends object>(fields: FieldValidators<T>) => ObjectValidator<T>;
|
|
22
|
-
static
|
|
24
|
+
static tuple: <T extends [...unknown[]]>(validators: TupleValidators<T>) => TupleValidator<T>;
|
|
25
|
+
static string: <T extends string = string>(options?: StringValidationOptions) => stringValidator<T>;
|
|
23
26
|
static any: <T>() => AnyValidator<T>;
|
|
27
|
+
static instanceof: <T>(instance: ConstructorType<T>, options: InstanceofValidationOptions<T>) => InstanceofValidator<T>;
|
|
24
28
|
static optional: <T>(val: Validator<Exclude<T, undefined | null>>) => OptionalValidator<T>;
|
|
25
29
|
static or: <T>(validators: OrValidators<T>) => OrValidator<T>;
|
|
26
30
|
}
|
package/index.js
CHANGED
|
@@ -11,6 +11,8 @@ const AnyValidator_1 = require("./types/util/AnyValidator");
|
|
|
11
11
|
const OrValidator_1 = require("./types/util/OrValidator");
|
|
12
12
|
const ArrayValidator_1 = require("./types/object/ArrayValidator");
|
|
13
13
|
const EnumValidator_1 = require("./types/object/EnumValidator");
|
|
14
|
+
const TupleValidator_1 = require("./types/object/TupleValidator");
|
|
15
|
+
const InstanceofValidator_1 = require("./types/util/InstanceofValidator");
|
|
14
16
|
var Validator_1 = require("./Validator");
|
|
15
17
|
Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return Validator_1.Validator; } });
|
|
16
18
|
var ErrorType_1 = require("./errors/ErrorType");
|
|
@@ -21,11 +23,13 @@ class v {
|
|
|
21
23
|
static boolean = () => new BooleanValidator_1.BooleanValidator();
|
|
22
24
|
static int = (options) => new IntValidator_1.IntValidator(options);
|
|
23
25
|
static number = (options) => new NumberValidator_1.NumberValidator(options);
|
|
24
|
-
static array = (item) => new ArrayValidator_1.ArrayValidator(item);
|
|
26
|
+
static array = (item, options) => new ArrayValidator_1.ArrayValidator(item, options);
|
|
25
27
|
static enum = (enumInstance) => new EnumValidator_1.EnumValidator(enumInstance);
|
|
26
28
|
static object = (fields) => new ObjectValidator_1.ObjectValidator(fields);
|
|
29
|
+
static tuple = (validators) => new TupleValidator_1.TupleValidator(validators);
|
|
27
30
|
static string = (options) => new StringValidator_1.stringValidator(options);
|
|
28
31
|
static any = () => new AnyValidator_1.AnyValidator();
|
|
32
|
+
static instanceof = (instance, options) => new InstanceofValidator_1.InstanceofValidator(instance, options);
|
|
29
33
|
static optional = (val) => new OptionalValidator_1.OptionalValidator(val);
|
|
30
34
|
static or = (validators) => new OrValidator_1.OrValidator(validators);
|
|
31
35
|
}
|
package/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oEAAiE;AACjE,8DAAsF;AACtF,oEAA+F;AAC/F,oEAAuF;AACvF,sEAAmE;AACnE,oEAA+F;AAE/F,4DAAyD;AACzD,0DAA0E;AAC1E,
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,oEAAiE;AACjE,8DAAsF;AACtF,oEAA+F;AAC/F,oEAAuF;AACvF,sEAAmE;AACnE,oEAA+F;AAE/F,4DAAyD;AACzD,0DAA0E;AAC1E,kEAA2F;AAC3F,gEAA4E;AAC5E,kEAAqF;AACrF,0EAA+H;AAE/H,yC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C;IAClG,MAAM,CAAC,EAAE,GAAG,CAAI,UAA2B,EAAE,EAAE,CAAC,IAAI,yBAAW,CAAC,UAAU,CAAC,CAAC;;AAhBvF,cAiBC"}
|
package/package.json
CHANGED
|
@@ -18,11 +18,11 @@ class IntValidator extends Validator_1.Validator {
|
|
|
18
18
|
}
|
|
19
19
|
// Minimum value check
|
|
20
20
|
if (this.options.min !== undefined && input < this.options.min) {
|
|
21
|
-
return this.createError(ErrorType_1.ErrorType.VALUE_BELOW, `Has to be a minimum value of ${this.options.min}
|
|
21
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_BELOW, `Has to be a minimum value of ${this.options.min}.`);
|
|
22
22
|
}
|
|
23
23
|
// Maximum value check
|
|
24
24
|
if (this.options.max !== undefined && this.options.max < input) {
|
|
25
|
-
return this.createError(ErrorType_1.ErrorType.VALUE_ABOVE, `Has to be a maximum value of ${this.options.max}
|
|
25
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_ABOVE, `Has to be a maximum value of ${this.options.max}.`);
|
|
26
26
|
}
|
|
27
27
|
return null;
|
|
28
28
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"IntValidator.js","sourceRoot":"","sources":["../../../src/types/number/IntValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAS5C,MAAa,YAAa,SAAQ,qBAAiB;IAGX;IAFpB,IAAI,GAAG,KAAK,CAAC;IAE7B,YAAoC,UAAgC,EAAE;QAClE,KAAK,EAAE,CAAC;QADwB,YAAO,GAAP,OAAO,CAA2B;IAEtE,CAAC;IAED,kBAAkB;IACF,cAAc,CAAC,KAAc;QACzC,cAAc;QACd,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,KAAK,KAAK,EAAE,CAAC;YAC3D,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,kBAAkB,CAAC,CAAC;QAC1E,CAA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C,OAAO,CAAC,GAAG,GAAG,CAAC,CAAA;QACvG,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAzBD,oCAyBC"}
|
|
@@ -18,11 +18,11 @@ class NumberValidator extends Validator_1.Validator {
|
|
|
18
18
|
}
|
|
19
19
|
// Minimum value check.
|
|
20
20
|
if (this.options.min !== undefined && (this.options.minExclusive ? input <= this.options.min : input < this.options.min)) {
|
|
21
|
-
return this.createError(this.options.minExclusive ? ErrorType_1.ErrorType.VALUE_BELOW_EXCLUSIVE : ErrorType_1.ErrorType.VALUE_BELOW, `Has to be a minimum value of ${this.options.min}${this.options.minExclusive ? " (exclusive)" : ""}
|
|
21
|
+
return this.createError(this.options.minExclusive ? ErrorType_1.ErrorType.VALUE_BELOW_EXCLUSIVE : ErrorType_1.ErrorType.VALUE_BELOW, `Has to be a minimum value of ${this.options.min}${this.options.minExclusive ? " (exclusive)" : ""}.`);
|
|
22
22
|
}
|
|
23
23
|
// Maximum value check.
|
|
24
24
|
if (this.options.max !== undefined && (this.options.maxExclusive ? this.options.max <= input : this.options.max < input)) {
|
|
25
|
-
return this.createError(this.options.maxExclusive ? ErrorType_1.ErrorType.VALUE_ABOVE_EXCLUSIVE : ErrorType_1.ErrorType.VALUE_ABOVE, `Has to be a maximum value of ${this.options.max}${this.options.minExclusive ? " (exclusive)" : ""}
|
|
25
|
+
return this.createError(this.options.maxExclusive ? ErrorType_1.ErrorType.VALUE_ABOVE_EXCLUSIVE : ErrorType_1.ErrorType.VALUE_ABOVE, `Has to be a maximum value of ${this.options.max}${this.options.minExclusive ? " (exclusive)" : ""}.`);
|
|
26
26
|
}
|
|
27
27
|
return null;
|
|
28
28
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"NumberValidator.js","sourceRoot":"","sources":["../../../src/types/number/NumberValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAa5C,MAAa,eAAgB,SAAQ,qBAAiB;IAGd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;AA/BD,0CA+BC"}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import type { ValidationError } from "../../errors/ValidationError";
|
|
2
2
|
import { Validator } from "../../Validator";
|
|
3
|
+
export interface ArrayValidatorOptions {
|
|
4
|
+
/** Minimum length of the array. */
|
|
5
|
+
min?: number;
|
|
6
|
+
/** Maximum length of the array. */
|
|
7
|
+
max?: number;
|
|
8
|
+
}
|
|
3
9
|
export declare class ArrayValidator<T> extends Validator<T[]> {
|
|
4
10
|
readonly item: Validator<T>;
|
|
11
|
+
private readonly options;
|
|
5
12
|
type: string;
|
|
6
|
-
constructor(item: Validator<T
|
|
13
|
+
constructor(item: Validator<T>, options?: ArrayValidatorOptions);
|
|
7
14
|
/** @inheritdoc */
|
|
8
15
|
validateReturn(input: unknown): ValidationError<T[]> | null;
|
|
9
16
|
}
|
|
@@ -5,28 +5,38 @@ const ErrorType_1 = require("../../errors/ErrorType");
|
|
|
5
5
|
const Validator_1 = require("../../Validator");
|
|
6
6
|
class ArrayValidator extends Validator_1.Validator {
|
|
7
7
|
item;
|
|
8
|
+
options;
|
|
8
9
|
type = "array";
|
|
9
|
-
constructor(item) {
|
|
10
|
+
constructor(item, options = {}) {
|
|
10
11
|
super();
|
|
11
12
|
this.item = item;
|
|
13
|
+
this.options = options;
|
|
12
14
|
}
|
|
13
15
|
/** @inheritdoc */
|
|
14
16
|
validateReturn(input) {
|
|
15
17
|
// Check type
|
|
16
18
|
if (typeof input !== "object" || input === null || !Array.isArray(input)) {
|
|
17
|
-
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "Has to be an
|
|
19
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "Has to be an array.");
|
|
20
|
+
}
|
|
21
|
+
// Check minimum length
|
|
22
|
+
if (this.options.min !== undefined && input.length < this.options.min) {
|
|
23
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_SHORT, `Has to have a minimum length of ${this.options.min}.`);
|
|
24
|
+
}
|
|
25
|
+
// Check maximum length
|
|
26
|
+
if (this.options.max !== undefined && input.length > this.options.max) {
|
|
27
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_SHORT, `Has to have a maximum length of ${this.options.max}.`);
|
|
18
28
|
}
|
|
19
29
|
// Check item type
|
|
20
30
|
let hasError = false;
|
|
21
|
-
const fields = Object.fromEntries(input.
|
|
31
|
+
const fields = Object.fromEntries(input.map((item, index) => {
|
|
22
32
|
const result = this.item.validateReturn(item);
|
|
23
33
|
if (result !== null) {
|
|
24
34
|
hasError = true;
|
|
25
35
|
}
|
|
26
|
-
return [
|
|
27
|
-
}));
|
|
36
|
+
return [index, result];
|
|
37
|
+
}).filter((entry) => !!entry[1]));
|
|
28
38
|
if (hasError) {
|
|
29
|
-
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "One or
|
|
39
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "One or more items are incorrect.", fields);
|
|
30
40
|
}
|
|
31
41
|
return null;
|
|
32
42
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ArrayValidator.js","sourceRoot":"","sources":["../../../src/types/object/ArrayValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;
|
|
1
|
+
{"version":3,"file":"ArrayValidator.js","sourceRoot":"","sources":["../../../src/types/object/ArrayValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAS5C,MAAa,cAAkB,SAAQ,qBAAc;IAI7B;IACE;IAJN,IAAI,GAAG,OAAO,CAAC;IAE/B,YACoB,IAAkB,EAChB,UAAiC,EAAE;QAErD,KAAK,EAAE,CAAC;QAHQ,SAAI,GAAJ,IAAI,CAAc;QAChB,YAAO,GAAP,OAAO,CAA4B;IAGzD,CAAC;IAED,kBAAkB;IACF,cAAc,CAAC,KAAc;QACzC,aAAa;QACb,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,CAAC,KA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WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,kCAAkC,EAAE,MAAM,CAAC,CAAC;QAClG,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AA1CD,wCA0CC"}
|
|
@@ -31,7 +31,7 @@ class ObjectValidator extends Validator_1.Validator {
|
|
|
31
31
|
}));
|
|
32
32
|
if (hasError || extraKeys.length) {
|
|
33
33
|
if (extraKeys.length) {
|
|
34
|
-
return this.createError(ErrorType_1.ErrorType.UNKNOWN_FIELD, `No extra keys allowed. Found [${extraKeys.join(", ")}]
|
|
34
|
+
return this.createError(ErrorType_1.ErrorType.UNKNOWN_FIELD, `No extra keys allowed. Found [${extraKeys.join(", ")}].`, hasError ? fields : undefined);
|
|
35
35
|
}
|
|
36
36
|
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "One or more fields are incorrect.", fields);
|
|
37
37
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ObjectValidator.js","sourceRoot":"","sources":["../../../src/types/object/ObjectValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAM5C,MAAa,eAAkC,SAAQ,qBAAY;IAG5B;IAFnB,IAAI,GAAG,QAAQ,CAAC;IAEhC,YAAmC,MAA0B;QACzD,KAAK,EAAE,CAAC;QADuB,WAAM,GAAN,MAAM,CAAoB;IAE7D,CAAC;IAED,kBAAkB;IACF,cAAc,CAAC,KAAc;QACzC,aAAa;QACb,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YAC9C,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,sBAAsB,CAAC,CAAC;QAC9E,CAAC;QAED,yBAAyB;QACzB,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;QAC3C,MAAM,SAAS,GAAG,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,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,EAAE,MAAM,CAAC,CAAC;QACnG,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CAEJ;AA1CD,0CA0CC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { ValidationError } from "../../errors/ValidationError";
|
|
2
|
+
import { Validator } from "../../Validator";
|
|
3
|
+
export type TupleValidators<T extends [...unknown[]]> = T extends [infer ITEM, ...infer ARR] ? [Validator<ITEM>, ...ARR extends never[] ? [] : TupleValidators<ARR>] : T extends ReadonlyArray<infer ARR> ? Array<Validator<ARR>> : never;
|
|
4
|
+
export declare class TupleValidator<T extends [...unknown[]]> extends Validator<T> {
|
|
5
|
+
readonly validators: TupleValidators<T>;
|
|
6
|
+
type: string;
|
|
7
|
+
constructor(validators: TupleValidators<T>);
|
|
8
|
+
validateReturn(input: unknown): ValidationError<T> | null;
|
|
9
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TupleValidator = void 0;
|
|
4
|
+
const ErrorType_1 = require("../../errors/ErrorType");
|
|
5
|
+
const Validator_1 = require("../../Validator");
|
|
6
|
+
class TupleValidator extends Validator_1.Validator {
|
|
7
|
+
validators;
|
|
8
|
+
type = "tuple";
|
|
9
|
+
constructor(validators) {
|
|
10
|
+
super();
|
|
11
|
+
this.validators = validators;
|
|
12
|
+
}
|
|
13
|
+
validateReturn(input) {
|
|
14
|
+
// Check type
|
|
15
|
+
if (typeof input !== "object" || !input || !Array.isArray(input)) {
|
|
16
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "Has te be a tuple.");
|
|
17
|
+
}
|
|
18
|
+
// Check length
|
|
19
|
+
if (input.length !== this.validators.length) {
|
|
20
|
+
return this.createError(input.length < this.validators.length
|
|
21
|
+
? ErrorType_1.ErrorType.VALUE_SHORT
|
|
22
|
+
: ErrorType_1.ErrorType.VALUE_LONG, `Has to have length of ${this.validators.length}.`);
|
|
23
|
+
}
|
|
24
|
+
// Check items
|
|
25
|
+
let hasError = false;
|
|
26
|
+
const fields = this.validators.map((validator, index) => {
|
|
27
|
+
const result = validator.validateReturn(input[index]);
|
|
28
|
+
if (result) {
|
|
29
|
+
hasError = true;
|
|
30
|
+
}
|
|
31
|
+
return result;
|
|
32
|
+
});
|
|
33
|
+
if (hasError) {
|
|
34
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, "One or more items are incorrect.", fields);
|
|
35
|
+
}
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.TupleValidator = TupleValidator;
|
|
40
|
+
//# sourceMappingURL=TupleValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TupleValidator.js","sourceRoot":"","sources":["../../../src/types/object/TupleValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAM5C,MAAa,cAAyC,SAAQ,qBAAY;IAGnC;IAFnB,IAAI,GAAG,OAAO,CAAC;IAE/B,YAAmC,UAA8B;QAC7D,KAAK,EAAE,CAAC;QADuB,eAAU,GAAV,UAAU,CAAoB;IAEjE,CAAC;IAEe,cAAc,CAAC,KAAc;QACzC,aAAa;QACb,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;YAC/D,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,oBAAoB,CAAC,CAAC;QAC5E,CAAC;QAED,eAAe;QACf,IAAI,KAAK,CAAC,MAAM,KAAK,IAAI,CAAC,UAAU,CAAC,MAAM,EAAE,CAAC;YAC1C,OAAO,IAAI,CAAC,WAAW,CACnB,KAAK,CAAC,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,MAAM;gBACjC,CAAC,CAAC,qBAAS,CAAC,WAAW;gBACvB,CAAC,CAAC,qBAAS,CAAC,UAAU,EAC1B,yBAAyB,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CACrD,CAAC;QACN,CAAC;QAED,cAAc;QACd,IAAI,QAAQ,GAAG,KAAK,CAAC;QACrB,MAAM,MAAM,GAAG,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,SAAS,EAAE,KAAK,EAAE,EAAE;YACpD,MAAM,MAAM,GAAG,SAAS,CAAC,cAAc,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAC;YACtD,IAAI,MAAM,EAAE,CAAC;gBACT,QAAQ,GAAG,IAAI,CAAC;YACpB,CAAC;YACD,OAAO,MAAM,CAAC;QAClB,CAAC,CAAmB,CAAC;QACrB,IAAI,QAAQ,EAAE,CAAC;YACX,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,kCAAkC,EAAE,MAAM,CAAC,CAAC;QAClG,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAtCD,wCAsCC"}
|
|
@@ -10,10 +10,10 @@ export interface StringValidationOptions {
|
|
|
10
10
|
/** Allowed values. (Will ignore all other options.) */
|
|
11
11
|
enum?: string[];
|
|
12
12
|
}
|
|
13
|
-
export declare class stringValidator extends Validator<
|
|
13
|
+
export declare class stringValidator<T extends string = string> extends Validator<T> {
|
|
14
14
|
private readonly options;
|
|
15
15
|
type: string;
|
|
16
16
|
constructor(options?: StringValidationOptions);
|
|
17
17
|
/** @inheritdoc */
|
|
18
|
-
validateReturn(input: unknown): ValidationError<
|
|
18
|
+
validateReturn(input: unknown): ValidationError<T> | null;
|
|
19
19
|
}
|
|
@@ -21,19 +21,19 @@ class stringValidator extends Validator_1.Validator {
|
|
|
21
21
|
if (!this.options.enum.includes(input)) {
|
|
22
22
|
return null;
|
|
23
23
|
}
|
|
24
|
-
return this.createError(ErrorType_1.ErrorType.NO_MATCH, `Has to match one of the known values [${this.options.enum.join(", ")}]
|
|
24
|
+
return this.createError(ErrorType_1.ErrorType.NO_MATCH, `Has to match one of the known values [${this.options.enum.join(", ")}].`);
|
|
25
25
|
}
|
|
26
26
|
// Minimum length check.
|
|
27
27
|
if (this.options.min !== undefined && input.length < this.options.min) {
|
|
28
|
-
return this.createError(ErrorType_1.ErrorType.VALUE_SHORT, `Has to have a minimum length of ${this.options.min}
|
|
28
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_SHORT, `Has to have a minimum length of ${this.options.min}.`);
|
|
29
29
|
}
|
|
30
30
|
// Maximum length check.
|
|
31
31
|
if (this.options.max !== undefined && this.options.max < input.length) {
|
|
32
|
-
return this.createError(ErrorType_1.ErrorType.VALUE_LONG, `Has to have a maximum length of ${this.options.max}
|
|
32
|
+
return this.createError(ErrorType_1.ErrorType.VALUE_LONG, `Has to have a maximum length of ${this.options.max}.`);
|
|
33
33
|
}
|
|
34
34
|
// Regex format check.
|
|
35
35
|
if (this.options.regex && !this.options.regex.test(input)) {
|
|
36
|
-
return this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, `Has to match format '${this.options.regex.source}'
|
|
36
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, `Has to match format '${this.options.regex.source}'.`);
|
|
37
37
|
}
|
|
38
38
|
return null;
|
|
39
39
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"StringValidator.js","sourceRoot":"","sources":["../../../src/types/string/StringValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAa5C,MAAa,
|
|
1
|
+
{"version":3,"file":"StringValidator.js","sourceRoot":"","sources":["../../../src/types/string/StringValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAa5C,MAAa,eAA2C,SAAQ,qBAAY;IAGpC;IAFpB,IAAI,GAAG,QAAQ,CAAC;IAEhC,YAAoC,UAAmC,EAAE;QACrE,KAAK,EAAE,CAAC;QADwB,YAAO,GAAP,OAAO,CAA8B;IAEzE,CAAC;IAED,kBA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AI,CAAC,WAAW,CAAC,qBAAS,CAAC,gBAAgB,EAAE,wBAAwB,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,MAAM,IAAI,CAAC,CAAC;QAC/G,CAAC;QACD,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AAzCD,0CAyCC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ValidationError } from "../../errors/ValidationError";
|
|
2
|
+
import { Validator } from "../../Validator";
|
|
3
|
+
export type ConstructorType<T> = {
|
|
4
|
+
new (...args: unknown[]): T;
|
|
5
|
+
name: string;
|
|
6
|
+
};
|
|
7
|
+
export interface InstanceofValidationOptions<T> {
|
|
8
|
+
/**
|
|
9
|
+
*
|
|
10
|
+
* @param value Value to be checked.
|
|
11
|
+
* @returns
|
|
12
|
+
* - `true`: When the value is correct.
|
|
13
|
+
* - `string`: Message when the value is incorrect.
|
|
14
|
+
*/
|
|
15
|
+
check?: (value: T) => true | string;
|
|
16
|
+
}
|
|
17
|
+
export declare class InstanceofValidator<T> extends Validator<T> {
|
|
18
|
+
readonly instance: ConstructorType<T>;
|
|
19
|
+
private readonly options;
|
|
20
|
+
type: string;
|
|
21
|
+
constructor(instance: ConstructorType<T>, options?: InstanceofValidationOptions<T>);
|
|
22
|
+
/** @inheritdoc */
|
|
23
|
+
validateReturn(input: unknown): ValidationError<T> | null;
|
|
24
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.InstanceofValidator = void 0;
|
|
4
|
+
const ErrorType_1 = require("../../errors/ErrorType");
|
|
5
|
+
const ValidationError_1 = require("../../errors/ValidationError");
|
|
6
|
+
const Validator_1 = require("../../Validator");
|
|
7
|
+
class InstanceofValidator extends Validator_1.Validator {
|
|
8
|
+
instance;
|
|
9
|
+
options;
|
|
10
|
+
type = "instanceof";
|
|
11
|
+
constructor(instance, options = {}) {
|
|
12
|
+
super();
|
|
13
|
+
this.instance = instance;
|
|
14
|
+
this.options = options;
|
|
15
|
+
}
|
|
16
|
+
/** @inheritdoc */
|
|
17
|
+
validateReturn(input) {
|
|
18
|
+
// Type check.
|
|
19
|
+
if (!(input instanceof this.instance)) {
|
|
20
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_TYPE, `Has to be instance of ${this.instance.name}.`);
|
|
21
|
+
}
|
|
22
|
+
// Check with custom test.
|
|
23
|
+
if (this.options.check) {
|
|
24
|
+
try {
|
|
25
|
+
const result = this.options.check(input);
|
|
26
|
+
if (result !== true) {
|
|
27
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, result);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (e) {
|
|
31
|
+
if (e instanceof ValidationError_1.ValidationError) {
|
|
32
|
+
return e;
|
|
33
|
+
}
|
|
34
|
+
else if (e instanceof Error) {
|
|
35
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, e.message);
|
|
36
|
+
}
|
|
37
|
+
return this, this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, "Unknown.");
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
exports.InstanceofValidator = InstanceofValidator;
|
|
44
|
+
//# sourceMappingURL=InstanceofValidator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"InstanceofValidator.js","sourceRoot":"","sources":["../../../src/types/util/InstanceofValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AACnD,kEAA+D;AAC/D,+CAA4C;AAkB5C,MAAa,mBAAuB,SAAQ,qBAAY;IAIhC;IACC;IAJL,IAAI,GAAG,YAAY,CAAC;IAEpC,YACoB,QAA4B,EAC3B,UAA0C,EAAE;QAE7D,KAAK,EAAE,CAAC;QAHQ,aAAQ,GAAR,QAAQ,CAAoB;QAC3B,YAAO,GAAP,OAAO,CAAqC;IAGjE,CAAC;IAED,kBAAkB;IACF,cAAc,CAAC,KAAc;QACzC,cAAc;QACd,IAAI,CAAC,CAAC,KAAK,YAAY,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC;YACpC,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,cAAc,EAAE,yBAAyB,IAAI,CAAC,QAAQ,CAAC,IAAI,GAAG,CAAC,CAAC;QACtG,CAAC;QAED,0BAA0B;QAC1B,IAAI,IAAI,CAAC,OAAO,CAAC,KAAK,EAAE,CAAC;YACrB,IAAI,CAAC;gBACD,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;gBACzC,IAAI,MAAM,KAAK,IAAI,EAAE,CAAC;oBAClB,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,gBAAgB,EAAE,MAAM,CAAC,CAAC;gBAChE,CAAC;YACL,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACT,IAAI,CAAC,YAAY,iCAAe,EAAE,CAAC;oBAC/B,OAAO,CAAC,CAAC;gBACb,CAAC;qBAAM,IAAI,CAAC,YAAY,KAAK,EAAE,CAAC;oBAC5B,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,gBAAgB,EAAE,CAAC,CAAC,OAAO,CAAC,CAAC;gBACnE,CAAC;gBACD,OAAO,IAAI,EAAC,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,gBAAgB,EAAE,UAAU,CAAC,CAAC;YACzE,CAAC;QACL,CAAC;QAED,OAAO,IAAI,CAAC;IAChB,CAAC;CACJ;AApCD,kDAoCC"}
|
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
import type { ValidationError } from "../../errors/ValidationError";
|
|
2
2
|
import { Validator } from "../../Validator";
|
|
3
|
+
export interface OptionalValidatorOptions {
|
|
4
|
+
only?: "undefined" | "null";
|
|
5
|
+
}
|
|
3
6
|
export declare class OptionalValidator<T> extends Validator<T> {
|
|
4
7
|
readonly child: Validator<Exclude<T, undefined | null>>;
|
|
8
|
+
private readonly options;
|
|
5
9
|
type: string;
|
|
6
|
-
constructor(child: Validator<Exclude<T, undefined | null
|
|
10
|
+
constructor(child: Validator<Exclude<T, undefined | null>>, options?: OptionalValidatorOptions);
|
|
7
11
|
/** @inheritdoc */
|
|
8
12
|
validateReturn(input: unknown): ValidationError<T> | null;
|
|
9
13
|
}
|
|
@@ -1,18 +1,24 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.OptionalValidator = void 0;
|
|
4
|
+
const ErrorType_1 = require("../../errors/ErrorType");
|
|
4
5
|
const Validator_1 = require("../../Validator");
|
|
5
6
|
class OptionalValidator extends Validator_1.Validator {
|
|
6
7
|
child;
|
|
8
|
+
options;
|
|
7
9
|
type = "optional";
|
|
8
|
-
constructor(child) {
|
|
10
|
+
constructor(child, options = {}) {
|
|
9
11
|
super();
|
|
10
12
|
this.child = child;
|
|
13
|
+
this.options = options;
|
|
11
14
|
}
|
|
12
15
|
/** @inheritdoc */
|
|
13
16
|
validateReturn(input) {
|
|
14
17
|
if (input === undefined || input === null) {
|
|
15
|
-
|
|
18
|
+
if (!this.options.only || input === (this.options.only === "null" ? null : undefined)) {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
return this.createError(ErrorType_1.ErrorType.INCORRECT_FORMAT, `Optional value has to be ${this.options.only}`);
|
|
16
22
|
}
|
|
17
23
|
return this.child.validateReturn(input);
|
|
18
24
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"OptionalValidator.js","sourceRoot":"","sources":["../../../src/types/util/OptionalValidator.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"OptionalValidator.js","sourceRoot":"","sources":["../../../src/types/util/OptionalValidator.ts"],"names":[],"mappings":";;;AAAA,sDAAmD;AAEnD,+CAA4C;AAM5C,MAAa,iBAAqB,SAAQ,qBAAY;IAI9B;IACC;IAJL,IAAI,GAAG,UAAU,CAAC;IAElC,YACoB,KAA8C,EAC7C,UAAoC,EAAE;QAEvD,KAAK,EAAE,CAAA;QAHS,UAAK,GAAL,KAAK,CAAyC;QAC7C,YAAO,GAAP,OAAO,CAA+B;IAG3D,CAAC;IAED,kBAAkB;IACF,cAAc,CAAC,KAAc;QACzC,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;YACxC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,IAAI,KAAK,KAAK,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,KAAK,MAAM,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,SAAS,CAAC,EAAE,CAAC;gBACpF,OAAO,IAAI,CAAC;YAChB,CAAC;YACD,OAAO,IAAI,CAAC,WAAW,CAAC,qBAAS,CAAC,gBAAgB,EAAE,4BAA4B,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC;QACzG,CAAC;QACD,OAAO,IAAI,CAAC,KAAK,CAAC,cAAc,CAAC,KAAK,CAAC,CAAC;IAC5C,CAAC;CACJ;AApBD,8CAoBC"}
|