justus 0.1.6 → 0.2.1
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/dist/{dts-generator.js → dts-generator.cjs} +6 -15
- package/dist/dts-generator.cjs.map +6 -0
- package/{dts-generator.d.ts → dist/dts-generator.d.ts} +1 -1
- package/dist/dts-generator.mjs +5 -23
- package/dist/dts-generator.mjs.map +2 -2
- package/dist/errors.cjs +113 -0
- package/dist/errors.cjs.map +6 -0
- package/dist/errors.d.ts +55 -0
- package/dist/errors.mjs +85 -0
- package/dist/errors.mjs.map +6 -0
- package/dist/index.cjs +133 -0
- package/dist/index.cjs.map +6 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.mjs +23 -817
- package/dist/index.mjs.map +3 -3
- package/dist/schema.cjs +43 -0
- package/dist/schema.cjs.map +6 -0
- package/dist/schema.d.ts +16 -0
- package/dist/schema.mjs +17 -0
- package/dist/schema.mjs.map +6 -0
- package/dist/types.cjs +60 -0
- package/dist/types.cjs.map +6 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.mjs +33 -0
- package/dist/types.mjs.map +6 -0
- package/dist/utilities.cjs +52 -0
- package/dist/utilities.cjs.map +6 -0
- package/dist/utilities.d.ts +7 -0
- package/dist/utilities.mjs +27 -0
- package/dist/utilities.mjs.map +6 -0
- package/dist/validators/any.cjs +39 -0
- package/dist/validators/any.cjs.map +6 -0
- package/dist/validators/any.d.ts +7 -0
- package/dist/validators/any.mjs +13 -0
- package/dist/validators/any.mjs.map +6 -0
- package/dist/validators/array.cjs +104 -0
- package/dist/validators/array.cjs.map +6 -0
- package/dist/validators/array.d.ts +32 -0
- package/dist/validators/array.mjs +76 -0
- package/dist/validators/array.mjs.map +6 -0
- package/dist/validators/boolean.cjs +56 -0
- package/dist/validators/boolean.cjs.map +6 -0
- package/dist/validators/boolean.d.ts +21 -0
- package/dist/validators/boolean.mjs +30 -0
- package/dist/validators/boolean.mjs.map +6 -0
- package/dist/validators/constant.cjs +51 -0
- package/dist/validators/constant.cjs.map +6 -0
- package/dist/validators/constant.d.ts +12 -0
- package/dist/validators/constant.mjs +24 -0
- package/dist/validators/constant.mjs.map +6 -0
- package/dist/validators/date.cjs +80 -0
- package/dist/validators/date.cjs.map +6 -0
- package/dist/validators/date.d.ts +22 -0
- package/dist/validators/date.mjs +54 -0
- package/dist/validators/date.mjs.map +6 -0
- package/dist/validators/never.cjs +44 -0
- package/dist/validators/never.cjs.map +6 -0
- package/dist/validators/never.d.ts +9 -0
- package/dist/validators/never.mjs +18 -0
- package/dist/validators/never.mjs.map +6 -0
- package/dist/validators/number.cjs +153 -0
- package/dist/validators/number.cjs.map +6 -0
- package/dist/validators/number.d.ts +48 -0
- package/dist/validators/number.mjs +126 -0
- package/dist/validators/number.mjs.map +6 -0
- package/dist/validators/object.cjs +129 -0
- package/dist/validators/object.cjs.map +6 -0
- package/dist/validators/object.d.ts +22 -0
- package/dist/validators/object.mjs +105 -0
- package/dist/validators/object.mjs.map +6 -0
- package/dist/validators/optional.cjs +61 -0
- package/dist/validators/optional.cjs.map +6 -0
- package/dist/validators/optional.d.ts +21 -0
- package/dist/validators/optional.mjs +35 -0
- package/dist/validators/optional.mjs.map +6 -0
- package/dist/validators/string.cjs +84 -0
- package/dist/validators/string.cjs.map +6 -0
- package/dist/validators/string.d.ts +35 -0
- package/dist/validators/string.mjs +57 -0
- package/dist/validators/string.mjs.map +6 -0
- package/dist/validators/tuple.cjs +95 -0
- package/dist/validators/tuple.cjs.map +6 -0
- package/dist/validators/tuple.d.ts +15 -0
- package/dist/validators/tuple.mjs +69 -0
- package/dist/validators/tuple.mjs.map +6 -0
- package/dist/validators/union.cjs +78 -0
- package/dist/validators/union.cjs.map +6 -0
- package/dist/validators/union.d.ts +22 -0
- package/dist/validators/union.mjs +50 -0
- package/dist/validators/union.mjs.map +6 -0
- package/dist/validators/url.cjs +117 -0
- package/dist/validators/url.cjs.map +6 -0
- package/dist/validators/url.d.ts +57 -0
- package/dist/validators/url.mjs +91 -0
- package/dist/validators/url.mjs.map +6 -0
- package/package.json +33 -23
- package/src/dts-generator.ts +6 -21
- package/src/index.ts +23 -23
- package/src/schema.ts +11 -9
- package/src/types.ts +42 -26
- package/src/utilities.ts +9 -6
- package/src/validators/array.ts +6 -5
- package/src/validators/boolean.ts +2 -2
- package/src/validators/constant.ts +3 -1
- package/src/validators/date.ts +1 -1
- package/src/validators/never.ts +4 -2
- package/src/validators/number.ts +8 -7
- package/src/validators/object.ts +15 -18
- package/src/validators/optional.ts +5 -7
- package/src/validators/string.ts +8 -6
- package/src/validators/tuple.ts +7 -5
- package/src/validators/union.ts +7 -5
- package/src/validators/url.ts +8 -7
- package/dist/dts-generator.js.map +0 -6
- package/dist/index.js +0 -948
- package/dist/index.js.map +0 -6
- package/index.d.ts +0 -618
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// validators/union.ts
|
|
21
|
+
var union_exports = {};
|
|
22
|
+
__export(union_exports, {
|
|
23
|
+
AllOfValidator: () => AllOfValidator,
|
|
24
|
+
OneOfValidator: () => OneOfValidator,
|
|
25
|
+
allOf: () => allOf,
|
|
26
|
+
oneOf: () => oneOf
|
|
27
|
+
});
|
|
28
|
+
module.exports = __toCommonJS(union_exports);
|
|
29
|
+
var import_errors = require("../errors.cjs");
|
|
30
|
+
var import_types = require("../types.cjs");
|
|
31
|
+
var import_utilities = require("../utilities.cjs");
|
|
32
|
+
var OneOfValidator = class extends import_types.AbstractValidator {
|
|
33
|
+
validators;
|
|
34
|
+
constructor(args) {
|
|
35
|
+
super();
|
|
36
|
+
this.validators = args.map((validation) => (0, import_utilities.getValidator)(validation));
|
|
37
|
+
(0, import_errors.assertSchema)(this.validators.length > 0, 'At least one validation required in "oneOf"');
|
|
38
|
+
}
|
|
39
|
+
validate(value, options) {
|
|
40
|
+
const builder = new import_errors.ValidationErrorBuilder();
|
|
41
|
+
for (const validator of this.validators) {
|
|
42
|
+
try {
|
|
43
|
+
return validator.validate(value, options);
|
|
44
|
+
} catch (error) {
|
|
45
|
+
builder.record(error);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return builder.assert(value);
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
function oneOf(...args) {
|
|
52
|
+
return new OneOfValidator(args);
|
|
53
|
+
}
|
|
54
|
+
var AllOfValidator = class extends import_types.AbstractValidator {
|
|
55
|
+
validators;
|
|
56
|
+
constructor(args) {
|
|
57
|
+
super();
|
|
58
|
+
this.validators = args.map((validation) => (0, import_utilities.getValidator)(validation));
|
|
59
|
+
(0, import_errors.assertSchema)(this.validators.length > 0, 'At least one validation required in "allOf"');
|
|
60
|
+
}
|
|
61
|
+
validate(value, options) {
|
|
62
|
+
for (const validator of this.validators) {
|
|
63
|
+
value = validator.validate(value, options);
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
function allOf(...args) {
|
|
69
|
+
return new AllOfValidator(args);
|
|
70
|
+
}
|
|
71
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
72
|
+
0 && (module.exports = {
|
|
73
|
+
AllOfValidator,
|
|
74
|
+
OneOfValidator,
|
|
75
|
+
allOf,
|
|
76
|
+
oneOf
|
|
77
|
+
});
|
|
78
|
+
//# sourceMappingURL=union.cjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/validators/union.ts"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAqD;AACrD,mBAAkC;AAClC,uBAA6B;AA2BtB,IAAM,iBAAN,cAAuD,+BAA+C;AAAA,EAClG;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,mBAAe,+BAAa,UAAU,CAAC;AACnE,oCAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAA0D;AACjF,UAAM,UAAU,IAAI,qCAAuB;AAC3C,eAAW,aAAa,KAAK,YAAY;AACvC,UAAI;AACF,eAAO,UAAU,SAAS,OAAO,OAAO;AAAA,MAC1C,SAAS,OAAP;AACA,gBAAQ,OAAO,KAAK;AAAA,MACtB;AAAA,IACF;AACA,WAAO,QAAQ,OAAO,KAAoC;AAAA,EAC5D;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;AAgBO,IAAM,iBAAN,cAAuD,+BAA+C;AAAA,EAClG;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,mBAAe,+BAAa,UAAU,CAAC;AACnE,oCAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAA0D;AACjF,eAAW,aAAa,KAAK,YAAY;AACvC,cAAQ,UAAU,SAAS,OAAO,OAAO;AAAA,IAC3C;AACA,WAAO;AAAA,EACT;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { AbstractValidator } from '../types';
|
|
2
|
+
import type { InferValidation, Validation, ValidationOptions, Validator } from '../types';
|
|
3
|
+
export type OneOfArguments = readonly Validation[];
|
|
4
|
+
export type InferOneOfValidationType<A extends OneOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends OneOfArguments ? InferValidation<First> | InferOneOfValidationType<Rest> : InferValidation<First> : never : A extends readonly (infer Type)[] ? Type extends Validation ? InferValidation<Type> : never : never;
|
|
5
|
+
/** A `Validator` validating a value as _one of_ the specified arguments. */
|
|
6
|
+
export declare class OneOfValidator<A extends OneOfArguments> extends AbstractValidator<InferOneOfValidationType<A>> {
|
|
7
|
+
readonly validators: readonly Validator[];
|
|
8
|
+
constructor(args: A);
|
|
9
|
+
validate(value: unknown, options?: ValidationOptions): InferOneOfValidationType<A>;
|
|
10
|
+
}
|
|
11
|
+
/** Validate a value as _one of_ the specified arguments */
|
|
12
|
+
export declare function oneOf<A extends OneOfArguments>(...args: A): OneOfValidator<A>;
|
|
13
|
+
export type AllOfArguments = readonly [Validation, ...Validation[]];
|
|
14
|
+
export type InferAllOfValidationType<A extends AllOfArguments> = A extends readonly [infer First, ...infer Rest] ? First extends Validation ? Rest extends AllOfArguments ? InferValidation<First> & InferAllOfValidationType<Rest> : InferValidation<First> : never : never;
|
|
15
|
+
/** A `Validator` validating a value as _all of_ the specified arguments. */
|
|
16
|
+
export declare class AllOfValidator<A extends AllOfArguments> extends AbstractValidator<InferAllOfValidationType<A>> {
|
|
17
|
+
readonly validators: readonly Validator[];
|
|
18
|
+
constructor(args: A);
|
|
19
|
+
validate(value: unknown, options?: ValidationOptions): InferAllOfValidationType<A>;
|
|
20
|
+
}
|
|
21
|
+
/** Validate a value as _all of_ the specified arguments */
|
|
22
|
+
export declare function allOf<A extends AllOfArguments>(...args: A): AllOfValidator<A>;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// validators/union.ts
|
|
2
|
+
import { assertSchema, ValidationErrorBuilder } from "../errors.mjs";
|
|
3
|
+
import { AbstractValidator } from "../types.mjs";
|
|
4
|
+
import { getValidator } from "../utilities.mjs";
|
|
5
|
+
var OneOfValidator = class extends AbstractValidator {
|
|
6
|
+
validators;
|
|
7
|
+
constructor(args) {
|
|
8
|
+
super();
|
|
9
|
+
this.validators = args.map((validation) => getValidator(validation));
|
|
10
|
+
assertSchema(this.validators.length > 0, 'At least one validation required in "oneOf"');
|
|
11
|
+
}
|
|
12
|
+
validate(value, options) {
|
|
13
|
+
const builder = new ValidationErrorBuilder();
|
|
14
|
+
for (const validator of this.validators) {
|
|
15
|
+
try {
|
|
16
|
+
return validator.validate(value, options);
|
|
17
|
+
} catch (error) {
|
|
18
|
+
builder.record(error);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return builder.assert(value);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
24
|
+
function oneOf(...args) {
|
|
25
|
+
return new OneOfValidator(args);
|
|
26
|
+
}
|
|
27
|
+
var AllOfValidator = class extends AbstractValidator {
|
|
28
|
+
validators;
|
|
29
|
+
constructor(args) {
|
|
30
|
+
super();
|
|
31
|
+
this.validators = args.map((validation) => getValidator(validation));
|
|
32
|
+
assertSchema(this.validators.length > 0, 'At least one validation required in "allOf"');
|
|
33
|
+
}
|
|
34
|
+
validate(value, options) {
|
|
35
|
+
for (const validator of this.validators) {
|
|
36
|
+
value = validator.validate(value, options);
|
|
37
|
+
}
|
|
38
|
+
return value;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
function allOf(...args) {
|
|
42
|
+
return new AllOfValidator(args);
|
|
43
|
+
}
|
|
44
|
+
export {
|
|
45
|
+
AllOfValidator,
|
|
46
|
+
OneOfValidator,
|
|
47
|
+
allOf,
|
|
48
|
+
oneOf
|
|
49
|
+
};
|
|
50
|
+
//# sourceMappingURL=union.mjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/validators/union.ts"],
|
|
4
|
+
"mappings": ";AAAA,SAAS,cAAc,8BAA8B;AACrD,SAAS,yBAAyB;AAClC,SAAS,oBAAoB;AA2BtB,IAAM,iBAAN,cAAuD,kBAA+C;AAAA,EAClG;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,eAAe,aAAa,UAAU,CAAC;AACnE,iBAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAA0D;AACjF,UAAM,UAAU,IAAI,uBAAuB;AAC3C,eAAW,aAAa,KAAK,YAAY;AACvC,UAAI;AACF,eAAO,UAAU,SAAS,OAAO,OAAO;AAAA,MAC1C,SAAS,OAAP;AACA,gBAAQ,OAAO,KAAK;AAAA,MACtB;AAAA,IACF;AACA,WAAO,QAAQ,OAAO,KAAoC;AAAA,EAC5D;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;AAgBO,IAAM,iBAAN,cAAuD,kBAA+C;AAAA,EAClG;AAAA,EAET,YAAY,MAAS;AACnB,UAAM;AACN,SAAK,aAAa,KAAK,IAAI,CAAC,eAAe,aAAa,UAAU,CAAC;AACnE,iBAAa,KAAK,WAAW,SAAS,GAAG,6CAA6C;AAAA,EACxF;AAAA,EAEA,SAAS,OAAgB,SAA0D;AACjF,eAAW,aAAa,KAAK,YAAY;AACvC,cAAQ,UAAU,SAAS,OAAO,OAAO;AAAA,IAC3C;AACA,WAAO;AAAA,EACT;AACF;AAGO,SAAS,SAAmC,MAA4B;AAC7E,SAAO,IAAI,eAAe,IAAI;AAChC;",
|
|
5
|
+
"names": []
|
|
6
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// validators/url.ts
|
|
21
|
+
var url_exports = {};
|
|
22
|
+
__export(url_exports, {
|
|
23
|
+
URLValidator: () => URLValidator,
|
|
24
|
+
url: () => url
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(url_exports);
|
|
27
|
+
var import_errors = require("../errors.cjs");
|
|
28
|
+
var import_types = require("../types.cjs");
|
|
29
|
+
var import_constant = require("./constant.cjs");
|
|
30
|
+
var import_object = require("./object.cjs");
|
|
31
|
+
var KEYS = [
|
|
32
|
+
"href",
|
|
33
|
+
"origin",
|
|
34
|
+
"protocol",
|
|
35
|
+
"username",
|
|
36
|
+
"password",
|
|
37
|
+
"host",
|
|
38
|
+
"hostname",
|
|
39
|
+
"port",
|
|
40
|
+
"pathname",
|
|
41
|
+
"search",
|
|
42
|
+
"hash"
|
|
43
|
+
];
|
|
44
|
+
var OPTIONS = {
|
|
45
|
+
stripAdditionalProperties: false,
|
|
46
|
+
stripForbiddenProperties: false,
|
|
47
|
+
stripOptionalNulls: false
|
|
48
|
+
};
|
|
49
|
+
var URLValidator = class extends import_types.AbstractValidator {
|
|
50
|
+
href;
|
|
51
|
+
origin;
|
|
52
|
+
protocol;
|
|
53
|
+
username;
|
|
54
|
+
password;
|
|
55
|
+
host;
|
|
56
|
+
hostname;
|
|
57
|
+
port;
|
|
58
|
+
pathname;
|
|
59
|
+
search;
|
|
60
|
+
hash;
|
|
61
|
+
searchParams;
|
|
62
|
+
constructor(constraints = {}) {
|
|
63
|
+
super();
|
|
64
|
+
for (const key of KEYS) {
|
|
65
|
+
const constraint = constraints[key];
|
|
66
|
+
if (typeof constraint === "string") {
|
|
67
|
+
this[key] = new import_constant.ConstantValidator(constraint);
|
|
68
|
+
} else if (constraint) {
|
|
69
|
+
this[key] = constraint;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (constraints.searchParams) {
|
|
73
|
+
this.searchParams = new import_object.ObjectValidator(constraints.searchParams);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
validate(value) {
|
|
77
|
+
let url2;
|
|
78
|
+
try {
|
|
79
|
+
url2 = value instanceof URL ? value : new URL(value);
|
|
80
|
+
} catch (error) {
|
|
81
|
+
throw new import_errors.ValidationError('Value could not be converted to a "URL"');
|
|
82
|
+
}
|
|
83
|
+
const builder = new import_errors.ValidationErrorBuilder();
|
|
84
|
+
for (const key of KEYS) {
|
|
85
|
+
const validator = this[key];
|
|
86
|
+
if (validator) {
|
|
87
|
+
try {
|
|
88
|
+
validator.validate(url2[key], OPTIONS);
|
|
89
|
+
} catch (error) {
|
|
90
|
+
builder.record(error, key);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (this.searchParams) {
|
|
95
|
+
const parameters = {};
|
|
96
|
+
for (const param of url2.searchParams.keys()) {
|
|
97
|
+
parameters[param] = url2.searchParams.get(param);
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
this.searchParams.validate(parameters, OPTIONS);
|
|
101
|
+
} catch (error) {
|
|
102
|
+
builder.record(error, "searchParams");
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return builder.assert(url2);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
function _url(constraints) {
|
|
109
|
+
return new URLValidator(constraints);
|
|
110
|
+
}
|
|
111
|
+
var url = (0, import_types.makeValidatorFactory)(new URLValidator(), _url);
|
|
112
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
113
|
+
0 && (module.exports = {
|
|
114
|
+
URLValidator,
|
|
115
|
+
url
|
|
116
|
+
});
|
|
117
|
+
//# sourceMappingURL=url.cjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/validators/url.ts"],
|
|
4
|
+
"mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAwD;AACxD,mBAAwD;AACxD,sBAAkC;AAClC,oBAAgC;AAKhC,IAAM,OAAwD;AAAA,EAC5D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,UAA6B;AAAA,EACjC,2BAA2B;AAAA,EAC3B,0BAA0B;AAAA,EAC1B,oBAAoB;AACtB;AAoCO,IAAM,eAAN,cAA2B,+BAAuB;AAAA,EAC9C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAET,YAAY,cAA8B,CAAC,GAAG;AAC5C,UAAM;AAEN,eAAW,OAAO,MAAM;AACtB,YAAM,aAAa,YAAY,GAAG;AAClC,UAAI,OAAO,eAAe,UAAU;AAClC,aAAK,GAAG,IAAI,IAAI,kCAAkB,UAAU;AAAA,MAC9C,WAAW,YAAY;AACrB,aAAK,GAAG,IAAI;AAAA,MACd;AAAA,IACF;AAEA,QAAI,YAAY,cAAc;AAC5B,WAAK,eAAe,IAAI,8BAAgB,YAAY,YAAY;AAAA,IAClE;AAAA,EACF;AAAA,EAEA,SAAS,OAAqB;AAC5B,QAAIA;AACJ,QAAI;AACF,MAAAA,OAAM,iBAAiB,MAAM,QAAQ,IAAI,IAAI,KAAY;AAAA,IAC3D,SAAS,OAAP;AACA,YAAM,IAAI,8BAAgB,yCAAyC;AAAA,IACrE;AAEA,UAAM,UAAU,IAAI,qCAAuB;AAE3C,eAAW,OAAO,MAAM;AACtB,YAAM,YAAY,KAAK,GAAG;AAC1B,UAAI,WAAW;AACb,YAAI;AACF,oBAAU,SAASA,KAAI,GAAG,GAAG,OAAO;AAAA,QACtC,SAAS,OAAP;AACA,kBAAQ,OAAO,OAAO,GAAG;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AAEA,QAAI,KAAK,cAAc;AACrB,YAAM,aAAqC,CAAC;AAC5C,iBAAW,SAASA,KAAI,aAAa,KAAK,GAAG;AAC3C,mBAAW,KAAK,IAAIA,KAAI,aAAa,IAAI,KAAK;AAAA,MAChD;AAEA,UAAI;AACF,aAAK,aAAa,SAAS,YAAY,OAAO;AAAA,MAChD,SAAS,OAAP;AACA,gBAAQ,OAAO,OAAO,cAAc;AAAA,MACtC;AAAA,IACF;AAEA,WAAO,QAAQ,OAAOA,IAAG;AAAA,EAC3B;AACF;AAEA,SAAS,KAAK,aAA2C;AACvD,SAAO,IAAI,aAAa,WAAW;AACrC;AAGO,IAAM,UAAM,mCAAqB,IAAI,aAAa,GAAG,IAAI;",
|
|
5
|
+
"names": ["url"]
|
|
6
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { AbstractValidator } from '../types';
|
|
3
|
+
import { ObjectValidator } from './object';
|
|
4
|
+
import type { Schema, Validator } from '../types';
|
|
5
|
+
/** Constraints to validate a `URL` with. */
|
|
6
|
+
export interface URLConstraints {
|
|
7
|
+
/** Constraint to validate the `href` component of the `URL`. */
|
|
8
|
+
href?: string | Validator<string>;
|
|
9
|
+
/** Constraint to validate the `origin` component of the `URL`. */
|
|
10
|
+
origin?: string | Validator<string>;
|
|
11
|
+
/** Constraint to validate the `protocol` component of the `URL`. */
|
|
12
|
+
protocol?: string | Validator<string>;
|
|
13
|
+
/** Constraint to validate the `username` component of the `URL`. */
|
|
14
|
+
username?: string | Validator<string>;
|
|
15
|
+
/** Constraint to validate the `password` component of the `URL`. */
|
|
16
|
+
password?: string | Validator<string>;
|
|
17
|
+
/** Constraint to validate the `host` (`hostname:port`) component of the `URL`. */
|
|
18
|
+
host?: string | Validator<string>;
|
|
19
|
+
/** Constraint to validate the `hostname` component of the `URL`. */
|
|
20
|
+
hostname?: string | Validator<string>;
|
|
21
|
+
/** Constraint to validate the `port` component of the `URL`. */
|
|
22
|
+
port?: string | Validator<string>;
|
|
23
|
+
/** Constraint to validate the `pathname` component of the `URL`. */
|
|
24
|
+
pathname?: string | Validator<string>;
|
|
25
|
+
/** Constraint to validate the `search` component of the `URL` as a string. */
|
|
26
|
+
search?: string | Validator<string>;
|
|
27
|
+
/** Constraint to validate the `hash` component of the `URL`. */
|
|
28
|
+
hash?: string | Validator<string>;
|
|
29
|
+
/**
|
|
30
|
+
* Schema used to validate the `searchParams` component of the `URL`.
|
|
31
|
+
*
|
|
32
|
+
* The `searchParams` will be normalized in a `Record<string, string>`, where
|
|
33
|
+
* only the _first_ value associated with a search parameter will be checked.
|
|
34
|
+
*/
|
|
35
|
+
searchParams?: Schema;
|
|
36
|
+
}
|
|
37
|
+
/** A `Validator` validating URLs and converting them to `URL` instances. */
|
|
38
|
+
export declare class URLValidator extends AbstractValidator<URL> {
|
|
39
|
+
readonly href?: Validator<string>;
|
|
40
|
+
readonly origin?: Validator<string>;
|
|
41
|
+
readonly protocol?: Validator<string>;
|
|
42
|
+
readonly username?: Validator<string>;
|
|
43
|
+
readonly password?: Validator<string>;
|
|
44
|
+
readonly host?: Validator<string>;
|
|
45
|
+
readonly hostname?: Validator<string>;
|
|
46
|
+
readonly port?: Validator<string>;
|
|
47
|
+
readonly pathname?: Validator<string>;
|
|
48
|
+
readonly search?: Validator<string>;
|
|
49
|
+
readonly hash?: Validator<string>;
|
|
50
|
+
readonly searchParams?: ObjectValidator<Schema>;
|
|
51
|
+
constructor(constraints?: URLConstraints);
|
|
52
|
+
validate(value: unknown): URL;
|
|
53
|
+
}
|
|
54
|
+
declare function _url(constraints: URLConstraints): URLValidator;
|
|
55
|
+
/** Validate URLs and convert them to `URL` instances. */
|
|
56
|
+
export declare const url: typeof _url & URLValidator;
|
|
57
|
+
export {};
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// validators/url.ts
|
|
2
|
+
import { ValidationError, ValidationErrorBuilder } from "../errors.mjs";
|
|
3
|
+
import { AbstractValidator, makeValidatorFactory } from "../types.mjs";
|
|
4
|
+
import { ConstantValidator } from "./constant.mjs";
|
|
5
|
+
import { ObjectValidator } from "./object.mjs";
|
|
6
|
+
var KEYS = [
|
|
7
|
+
"href",
|
|
8
|
+
"origin",
|
|
9
|
+
"protocol",
|
|
10
|
+
"username",
|
|
11
|
+
"password",
|
|
12
|
+
"host",
|
|
13
|
+
"hostname",
|
|
14
|
+
"port",
|
|
15
|
+
"pathname",
|
|
16
|
+
"search",
|
|
17
|
+
"hash"
|
|
18
|
+
];
|
|
19
|
+
var OPTIONS = {
|
|
20
|
+
stripAdditionalProperties: false,
|
|
21
|
+
stripForbiddenProperties: false,
|
|
22
|
+
stripOptionalNulls: false
|
|
23
|
+
};
|
|
24
|
+
var URLValidator = class extends AbstractValidator {
|
|
25
|
+
href;
|
|
26
|
+
origin;
|
|
27
|
+
protocol;
|
|
28
|
+
username;
|
|
29
|
+
password;
|
|
30
|
+
host;
|
|
31
|
+
hostname;
|
|
32
|
+
port;
|
|
33
|
+
pathname;
|
|
34
|
+
search;
|
|
35
|
+
hash;
|
|
36
|
+
searchParams;
|
|
37
|
+
constructor(constraints = {}) {
|
|
38
|
+
super();
|
|
39
|
+
for (const key of KEYS) {
|
|
40
|
+
const constraint = constraints[key];
|
|
41
|
+
if (typeof constraint === "string") {
|
|
42
|
+
this[key] = new ConstantValidator(constraint);
|
|
43
|
+
} else if (constraint) {
|
|
44
|
+
this[key] = constraint;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
if (constraints.searchParams) {
|
|
48
|
+
this.searchParams = new ObjectValidator(constraints.searchParams);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
validate(value) {
|
|
52
|
+
let url2;
|
|
53
|
+
try {
|
|
54
|
+
url2 = value instanceof URL ? value : new URL(value);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
throw new ValidationError('Value could not be converted to a "URL"');
|
|
57
|
+
}
|
|
58
|
+
const builder = new ValidationErrorBuilder();
|
|
59
|
+
for (const key of KEYS) {
|
|
60
|
+
const validator = this[key];
|
|
61
|
+
if (validator) {
|
|
62
|
+
try {
|
|
63
|
+
validator.validate(url2[key], OPTIONS);
|
|
64
|
+
} catch (error) {
|
|
65
|
+
builder.record(error, key);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
if (this.searchParams) {
|
|
70
|
+
const parameters = {};
|
|
71
|
+
for (const param of url2.searchParams.keys()) {
|
|
72
|
+
parameters[param] = url2.searchParams.get(param);
|
|
73
|
+
}
|
|
74
|
+
try {
|
|
75
|
+
this.searchParams.validate(parameters, OPTIONS);
|
|
76
|
+
} catch (error) {
|
|
77
|
+
builder.record(error, "searchParams");
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
return builder.assert(url2);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
function _url(constraints) {
|
|
84
|
+
return new URLValidator(constraints);
|
|
85
|
+
}
|
|
86
|
+
var url = makeValidatorFactory(new URLValidator(), _url);
|
|
87
|
+
export {
|
|
88
|
+
URLValidator,
|
|
89
|
+
url
|
|
90
|
+
};
|
|
91
|
+
//# sourceMappingURL=url.mjs.map
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../../src/validators/url.ts"],
|
|
4
|
+
"mappings": ";AAAA,SAAS,iBAAiB,8BAA8B;AACxD,SAAS,mBAAmB,4BAA4B;AACxD,SAAS,yBAAyB;AAClC,SAAS,uBAAuB;AAKhC,IAAM,OAAwD;AAAA,EAC5D;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF;AAEA,IAAM,UAA6B;AAAA,EACjC,2BAA2B;AAAA,EAC3B,0BAA0B;AAAA,EAC1B,oBAAoB;AACtB;AAoCO,IAAM,eAAN,cAA2B,kBAAuB;AAAA,EAC9C;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EAEA;AAAA,EAET,YAAY,cAA8B,CAAC,GAAG;AAC5C,UAAM;AAEN,eAAW,OAAO,MAAM;AACtB,YAAM,aAAa,YAAY,GAAG;AAClC,UAAI,OAAO,eAAe,UAAU;AAClC,aAAK,GAAG,IAAI,IAAI,kBAAkB,UAAU;AAAA,MAC9C,WAAW,YAAY;AACrB,aAAK,GAAG,IAAI;AAAA,MACd;AAAA,IACF;AAEA,QAAI,YAAY,cAAc;AAC5B,WAAK,eAAe,IAAI,gBAAgB,YAAY,YAAY;AAAA,IAClE;AAAA,EACF;AAAA,EAEA,SAAS,OAAqB;AAC5B,QAAIA;AACJ,QAAI;AACF,MAAAA,OAAM,iBAAiB,MAAM,QAAQ,IAAI,IAAI,KAAY;AAAA,IAC3D,SAAS,OAAP;AACA,YAAM,IAAI,gBAAgB,yCAAyC;AAAA,IACrE;AAEA,UAAM,UAAU,IAAI,uBAAuB;AAE3C,eAAW,OAAO,MAAM;AACtB,YAAM,YAAY,KAAK,GAAG;AAC1B,UAAI,WAAW;AACb,YAAI;AACF,oBAAU,SAASA,KAAI,GAAG,GAAG,OAAO;AAAA,QACtC,SAAS,OAAP;AACA,kBAAQ,OAAO,OAAO,GAAG;AAAA,QAC3B;AAAA,MACF;AAAA,IACF;AAEA,QAAI,KAAK,cAAc;AACrB,YAAM,aAAqC,CAAC;AAC5C,iBAAW,SAASA,KAAI,aAAa,KAAK,GAAG;AAC3C,mBAAW,KAAK,IAAIA,KAAI,aAAa,IAAI,KAAK;AAAA,MAChD;AAEA,UAAI;AACF,aAAK,aAAa,SAAS,YAAY,OAAO;AAAA,MAChD,SAAS,OAAP;AACA,gBAAQ,OAAO,OAAO,cAAc;AAAA,MACtC;AAAA,IACF;AAEA,WAAO,QAAQ,OAAOA,IAAG;AAAA,EAC3B;AACF;AAEA,SAAS,KAAK,aAA2C;AACvD,SAAO,IAAI,aAAa,WAAW;AACrC;AAGO,IAAM,MAAM,qBAAqB,IAAI,aAAa,GAAG,IAAI;",
|
|
5
|
+
"names": ["url"]
|
|
6
|
+
}
|
package/package.json
CHANGED
|
@@ -1,42 +1,47 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "justus",
|
|
3
|
-
"version": "0.1
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"description": "A JavaScript validation library, with types!",
|
|
5
|
-
"main": "./dist/index.
|
|
5
|
+
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.mjs",
|
|
7
7
|
"exports": {
|
|
8
8
|
".": {
|
|
9
|
-
"
|
|
10
|
-
|
|
9
|
+
"require": {
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"default": "./dist/index.cjs"
|
|
12
|
+
},
|
|
13
|
+
"import": {
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"default": "./dist/index.mjs"
|
|
16
|
+
}
|
|
11
17
|
},
|
|
12
18
|
"./dts-generator": {
|
|
13
|
-
"
|
|
14
|
-
|
|
19
|
+
"require": {
|
|
20
|
+
"types": "./dist/dts-generator.d.ts",
|
|
21
|
+
"default": "./dist/dts-generator.cjs"
|
|
22
|
+
},
|
|
23
|
+
"import": {
|
|
24
|
+
"types": "./dist/dts-generator.d.ts",
|
|
25
|
+
"default": "./dist/dts-generator.mjs"
|
|
26
|
+
}
|
|
15
27
|
}
|
|
16
28
|
},
|
|
17
|
-
"types": "./index.d.ts",
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
18
30
|
"scripts": {
|
|
19
|
-
"build": "
|
|
20
|
-
"
|
|
21
|
-
"
|
|
22
|
-
"
|
|
31
|
+
"build": "plug",
|
|
32
|
+
"coverage": "plug coverage",
|
|
33
|
+
"dev": "plug coverage -w src -w test",
|
|
34
|
+
"lint": "plug lint",
|
|
35
|
+
"test": "plug test",
|
|
36
|
+
"transpile": "plug transpile"
|
|
23
37
|
},
|
|
24
38
|
"author": "Juit Developers <developers@juit.com>",
|
|
25
39
|
"license": "MIT",
|
|
26
40
|
"devDependencies": {
|
|
27
|
-
"@
|
|
41
|
+
"@plugjs/build": "^0.2.4",
|
|
42
|
+
"@plugjs/tsd": "^0.2.3",
|
|
28
43
|
"@types/chai": "^4.3.4",
|
|
29
|
-
"@types/mocha": "^10.0.1",
|
|
30
|
-
"@types/node": "~16.18.12",
|
|
31
|
-
"@typescript-eslint/eslint-plugin": "^5.52.0",
|
|
32
|
-
"@typescript-eslint/parser": "^5.52.0",
|
|
33
44
|
"chai": "^4.3.7",
|
|
34
|
-
"esbuild": "^0.17.9",
|
|
35
|
-
"eslint": "^8.34.0",
|
|
36
|
-
"eslint-config-google": "^0.14.0",
|
|
37
|
-
"mocha": "^10.2.0",
|
|
38
|
-
"nodemon": "^2.0.20",
|
|
39
|
-
"nyc": "^15.1.0",
|
|
40
45
|
"tsd": "^0.25.0",
|
|
41
46
|
"typescript": "^4.9.5"
|
|
42
47
|
},
|
|
@@ -55,5 +60,10 @@
|
|
|
55
60
|
"bugs": {
|
|
56
61
|
"url": "https://github.com/juitnow/justus/issues"
|
|
57
62
|
},
|
|
58
|
-
"homepage": "https://github.com/juitnow/justus#readme"
|
|
63
|
+
"homepage": "https://github.com/juitnow/justus#readme",
|
|
64
|
+
"files": [
|
|
65
|
+
"*.md",
|
|
66
|
+
"dist/",
|
|
67
|
+
"src/"
|
|
68
|
+
]
|
|
59
69
|
}
|
package/src/dts-generator.ts
CHANGED
|
@@ -1,34 +1,29 @@
|
|
|
1
|
-
import ts
|
|
1
|
+
import ts from 'typescript'
|
|
2
|
+
|
|
2
3
|
import {
|
|
3
4
|
AllOfValidator,
|
|
4
|
-
any,
|
|
5
5
|
AnyArrayValidator,
|
|
6
6
|
AnyNumberValidator,
|
|
7
7
|
AnyObjectValidator,
|
|
8
8
|
AnyStringValidator,
|
|
9
9
|
AnyValidator,
|
|
10
|
-
array,
|
|
11
10
|
ArrayValidator,
|
|
12
11
|
assertSchema,
|
|
13
|
-
boolean,
|
|
14
12
|
BooleanValidator,
|
|
15
13
|
ConstantValidator,
|
|
16
|
-
date,
|
|
17
14
|
DateValidator,
|
|
18
15
|
getValidator,
|
|
19
|
-
never,
|
|
20
16
|
NeverValidator,
|
|
21
|
-
number,
|
|
22
17
|
NumberValidator,
|
|
23
|
-
object,
|
|
24
18
|
ObjectValidator,
|
|
25
19
|
OneOfValidator,
|
|
26
20
|
OptionalValidator,
|
|
27
|
-
string,
|
|
28
21
|
StringValidator,
|
|
29
22
|
TupleValidator,
|
|
30
|
-
url,
|
|
31
23
|
URLValidator,
|
|
24
|
+
} from './index'
|
|
25
|
+
|
|
26
|
+
import type {
|
|
32
27
|
Validation,
|
|
33
28
|
Validator,
|
|
34
29
|
} from './index'
|
|
@@ -128,7 +123,7 @@ function generateTypeNode(
|
|
|
128
123
|
if (type === neverType) return undefinedType
|
|
129
124
|
|
|
130
125
|
// If the type is already a union type, we simply add our "undefined"
|
|
131
|
-
if (isUnionTypeNode(type)) {
|
|
126
|
+
if (ts.isUnionTypeNode(type)) {
|
|
132
127
|
return ts.factory.createUnionTypeNode([ ...type.types, undefinedType ])
|
|
133
128
|
}
|
|
134
129
|
|
|
@@ -172,16 +167,6 @@ registerTypeGenerator(NeverValidator, () => neverType)
|
|
|
172
167
|
registerTypeGenerator(DateValidator, () => ts.factory.createTypeReferenceNode('Date'))
|
|
173
168
|
registerTypeGenerator(URLValidator, () => ts.factory.createTypeReferenceNode('URL'))
|
|
174
169
|
|
|
175
|
-
registerTypeGenerator(any, () => anyType)
|
|
176
|
-
registerTypeGenerator(array, () => anyArrayType)
|
|
177
|
-
registerTypeGenerator(number, () => numberType)
|
|
178
|
-
registerTypeGenerator(object, () => recordType)
|
|
179
|
-
registerTypeGenerator(string, () => stringType)
|
|
180
|
-
registerTypeGenerator(boolean, () => booleanType)
|
|
181
|
-
registerTypeGenerator(never, () => neverType)
|
|
182
|
-
registerTypeGenerator(date, () => ts.factory.createTypeReferenceNode('Date'))
|
|
183
|
-
registerTypeGenerator(url, () => ts.factory.createTypeReferenceNode('URL'))
|
|
184
|
-
|
|
185
170
|
/* ========================================================================== */
|
|
186
171
|
|
|
187
172
|
// Complex generator functions...
|