justus 0.1.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.
Files changed (116) hide show
  1. package/dist/{dts-generator.js → dts-generator.cjs} +3 -3
  2. package/dist/{dts-generator.js.map → dts-generator.cjs.map} +1 -1
  3. package/{dts-generator.d.ts → dist/dts-generator.d.ts} +1 -1
  4. package/dist/dts-generator.mjs +1 -1
  5. package/dist/dts-generator.mjs.map +1 -1
  6. package/dist/errors.cjs +113 -0
  7. package/dist/errors.cjs.map +6 -0
  8. package/dist/errors.d.ts +55 -0
  9. package/dist/errors.mjs +85 -0
  10. package/dist/errors.mjs.map +6 -0
  11. package/dist/index.cjs +133 -0
  12. package/dist/index.cjs.map +6 -0
  13. package/dist/index.d.ts +47 -0
  14. package/dist/index.mjs +23 -818
  15. package/dist/index.mjs.map +3 -3
  16. package/dist/schema.cjs +44 -0
  17. package/dist/schema.cjs.map +6 -0
  18. package/dist/schema.d.ts +16 -0
  19. package/dist/schema.mjs +18 -0
  20. package/dist/schema.mjs.map +6 -0
  21. package/dist/types.cjs +67 -0
  22. package/dist/types.cjs.map +6 -0
  23. package/dist/types.d.ts +136 -0
  24. package/dist/types.mjs +36 -0
  25. package/dist/types.mjs.map +6 -0
  26. package/dist/utilities.cjs +54 -0
  27. package/dist/utilities.cjs.map +6 -0
  28. package/dist/utilities.d.ts +7 -0
  29. package/dist/utilities.mjs +29 -0
  30. package/dist/utilities.mjs.map +6 -0
  31. package/dist/validators/any.cjs +39 -0
  32. package/dist/validators/any.cjs.map +6 -0
  33. package/dist/validators/any.d.ts +7 -0
  34. package/dist/validators/any.mjs +13 -0
  35. package/dist/validators/any.mjs.map +6 -0
  36. package/dist/validators/array.cjs +104 -0
  37. package/dist/validators/array.cjs.map +6 -0
  38. package/dist/validators/array.d.ts +32 -0
  39. package/dist/validators/array.mjs +76 -0
  40. package/dist/validators/array.mjs.map +6 -0
  41. package/dist/validators/boolean.cjs +56 -0
  42. package/dist/validators/boolean.cjs.map +6 -0
  43. package/dist/validators/boolean.d.ts +21 -0
  44. package/dist/validators/boolean.mjs +30 -0
  45. package/dist/validators/boolean.mjs.map +6 -0
  46. package/dist/validators/constant.cjs +51 -0
  47. package/dist/validators/constant.cjs.map +6 -0
  48. package/dist/validators/constant.d.ts +12 -0
  49. package/dist/validators/constant.mjs +24 -0
  50. package/dist/validators/constant.mjs.map +6 -0
  51. package/dist/validators/date.cjs +80 -0
  52. package/dist/validators/date.cjs.map +6 -0
  53. package/dist/validators/date.d.ts +22 -0
  54. package/dist/validators/date.mjs +54 -0
  55. package/dist/validators/date.mjs.map +6 -0
  56. package/dist/validators/never.cjs +44 -0
  57. package/dist/validators/never.cjs.map +6 -0
  58. package/dist/validators/never.d.ts +9 -0
  59. package/dist/validators/never.mjs +18 -0
  60. package/dist/validators/never.mjs.map +6 -0
  61. package/dist/validators/number.cjs +153 -0
  62. package/dist/validators/number.cjs.map +6 -0
  63. package/dist/validators/number.d.ts +48 -0
  64. package/dist/validators/number.mjs +126 -0
  65. package/dist/validators/number.mjs.map +6 -0
  66. package/dist/validators/object.cjs +129 -0
  67. package/dist/validators/object.cjs.map +6 -0
  68. package/dist/validators/object.d.ts +22 -0
  69. package/dist/validators/object.mjs +108 -0
  70. package/dist/validators/object.mjs.map +6 -0
  71. package/dist/validators/optional.cjs +61 -0
  72. package/dist/validators/optional.cjs.map +6 -0
  73. package/dist/validators/optional.d.ts +21 -0
  74. package/dist/validators/optional.mjs +35 -0
  75. package/dist/validators/optional.mjs.map +6 -0
  76. package/dist/validators/string.cjs +84 -0
  77. package/dist/validators/string.cjs.map +6 -0
  78. package/dist/validators/string.d.ts +35 -0
  79. package/dist/validators/string.mjs +57 -0
  80. package/dist/validators/string.mjs.map +6 -0
  81. package/dist/validators/tuple.cjs +95 -0
  82. package/dist/validators/tuple.cjs.map +6 -0
  83. package/dist/validators/tuple.d.ts +15 -0
  84. package/dist/validators/tuple.mjs +69 -0
  85. package/dist/validators/tuple.mjs.map +6 -0
  86. package/dist/validators/union.cjs +78 -0
  87. package/dist/validators/union.cjs.map +6 -0
  88. package/dist/validators/union.d.ts +22 -0
  89. package/dist/validators/union.mjs +50 -0
  90. package/dist/validators/union.mjs.map +6 -0
  91. package/dist/validators/url.cjs +117 -0
  92. package/dist/validators/url.cjs.map +6 -0
  93. package/dist/validators/url.d.ts +57 -0
  94. package/dist/validators/url.mjs +91 -0
  95. package/dist/validators/url.mjs.map +6 -0
  96. package/package.json +33 -23
  97. package/src/dts-generator.ts +4 -0
  98. package/src/index.ts +23 -23
  99. package/src/schema.ts +7 -5
  100. package/src/types.ts +51 -32
  101. package/src/utilities.ts +6 -2
  102. package/src/validators/array.ts +6 -5
  103. package/src/validators/boolean.ts +2 -2
  104. package/src/validators/constant.ts +3 -1
  105. package/src/validators/date.ts +1 -1
  106. package/src/validators/never.ts +4 -2
  107. package/src/validators/number.ts +8 -7
  108. package/src/validators/object.ts +12 -12
  109. package/src/validators/optional.ts +5 -8
  110. package/src/validators/string.ts +8 -6
  111. package/src/validators/tuple.ts +5 -3
  112. package/src/validators/union.ts +7 -5
  113. package/src/validators/url.ts +8 -7
  114. package/dist/index.js +0 -949
  115. package/dist/index.js.map +0 -6
  116. package/index.d.ts +0 -617
@@ -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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AG,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;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K;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.5",
3
+ "version": "0.2.0",
4
4
  "description": "A JavaScript validation library, with types!",
5
- "main": "./dist/index.js",
5
+ "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.mjs",
7
7
  "exports": {
8
8
  ".": {
9
- "import": "./dist/index.mjs",
10
- "require": "./dist/index.js"
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
- "import": "./dist/dts-generator.mjs",
14
- "require": "./dist/dts-generator.js"
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": "./build.sh",
20
- "dev": "nodemon -e ts -x ./build.sh -w src -w test -w test-d",
21
- "lint": "eslint src test test-d",
22
- "prepare": "patch -N -p0 -i .nyc.patch || true"
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
- "@microsoft/api-extractor": "^7.34.4",
41
+ "@plugjs/build": "^0.2.2",
42
+ "@plugjs/tsd": "^0.2.1",
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.51.0",
32
- "@typescript-eslint/parser": "^5.51.0",
33
44
  "chai": "^4.3.7",
34
- "esbuild": "^0.17.8",
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
  }
@@ -1,4 +1,5 @@
1
1
  import ts, { isUnionTypeNode } from 'typescript'
2
+
2
3
  import {
3
4
  AllOfValidator,
4
5
  any,
@@ -29,6 +30,9 @@ import {
29
30
  TupleValidator,
30
31
  url,
31
32
  URLValidator,
33
+ } from './index'
34
+
35
+ import type {
32
36
  Validation,
33
37
  Validator,
34
38
  } from './index'
package/src/index.ts CHANGED
@@ -9,31 +9,37 @@ export * from './types'
9
9
  export * from './utilities'
10
10
 
11
11
  // Validators
12
- export { allOf, oneOf, AllOfArguments, AllOfValidator, InferAllOfValidationType, InferOneOfValidationType, OneOfArguments, OneOfValidator } from './validators/union'
13
12
  export { any, AnyValidator } from './validators/any'
14
- export { _array, array, arrayOf, AnyArrayValidator, ArrayConstraints, ArrayValidator } from './validators/array'
15
- export { _boolean, boolean, BooleanConstraints, BooleanValidator } from './validators/boolean'
13
+ export { array, arrayOf, ArrayValidator, AnyArrayValidator } from './validators/array'
14
+ export { boolean, BooleanValidator } from './validators/boolean'
16
15
  export { constant, ConstantValidator } from './validators/constant'
17
- export { _date, date, DateConstraints, DateValidator } from './validators/date'
16
+ export { date, DateValidator } from './validators/date'
18
17
  export { never, NeverValidator } from './validators/never'
19
- export { _number, number, AnyNumberValidator, BrandedNumberConstraints, NumberConstraints, NumberValidator } from './validators/number'
20
- export { _object, object, objectOf, AnyObjectValidator, ObjectValidator } from './validators/object'
18
+ export { AnyNumberValidator, number, NumberValidator } from './validators/number'
19
+ export { AnyObjectValidator, object, objectOf, ObjectValidator } from './validators/object'
21
20
  export { optional, OptionalValidator } from './validators/optional'
22
- export { _string, string, AnyStringValidator, BrandedStringConstraints, StringConstraints, StringValidator } from './validators/string'
23
- export { tuple, TupleMember, TupleValidator } from './validators/tuple'
24
- export { _url, url, URLConstraints, URLValidator } from './validators/url'
21
+ export { AnyStringValidator, string, StringValidator } from './validators/string'
22
+ export { tuple, TupleValidator } from './validators/tuple'
23
+ export { allOf, AllOfValidator, oneOf, OneOfValidator } from './validators/union'
24
+ export { url, URLValidator } from './validators/url'
25
+
26
+ // Validator Types
27
+ export type { ArrayConstraints } from './validators/array'
28
+ export type { BooleanConstraints } from './validators/boolean'
29
+ export type { DateConstraints } from './validators/date'
30
+ export type { BrandedNumberConstraints } from './validators/number'
31
+ export type { BrandedStringConstraints, StringConstraints } from './validators/string'
32
+ export type { TupleMember } from './validators/tuple'
33
+ export type { URLConstraints } from './validators/url'
25
34
 
26
35
  /* ========================================================================== *
27
36
  * VALIDATE FUNCTION (our main entry point) *
28
37
  * ========================================================================== */
29
38
 
30
- import type { InferValidation, Validation, ValidationOptions } from './types'
39
+ import { defaultValidationOptions } from './types'
31
40
  import { getValidator } from './utilities'
32
41
 
33
- /** Options for `validate` */
34
- export type ValidateOptions = {
35
- -readonly [ key in keyof ValidationOptions ]?: ValidationOptions[key] | undefined
36
- }
42
+ import type { InferValidation, Validation, ValidationOptions } from './types'
37
43
 
38
44
  /**
39
45
  * Validate a _value_ using the specified `Validation`.
@@ -44,15 +50,9 @@ export type ValidateOptions = {
44
50
  export function validate<V extends Validation>(
45
51
  validation: V,
46
52
  value: any,
47
- options: ValidateOptions = {},
53
+ options?: ValidationOptions,
48
54
  ): InferValidation<V> {
49
- const opts: ValidationOptions = {
50
- stripAdditionalProperties: false,
51
- stripForbiddenProperties: false,
52
- stripOptionalNulls: false,
53
- ...options,
54
- }
55
-
55
+ const opts: ValidationOptions = { ...defaultValidationOptions, ...options }
56
56
  return getValidator(validation).validate(value, opts)
57
57
  }
58
58
 
@@ -73,7 +73,7 @@ export function validate<V extends Validation>(
73
73
  export function strip<V extends Validation>(
74
74
  validation: V,
75
75
  value: any,
76
- options: ValidateOptions = {},
76
+ options?: ValidationOptions,
77
77
  ): InferValidation<V> {
78
78
  const opts: ValidationOptions = {
79
79
  stripAdditionalProperties: true,
package/src/schema.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { any } from './validators/any'
1
+ import { additionalValidator } from './types'
2
+ // eslint-disable-next-line import/no-cycle
2
3
  import { getValidator } from './utilities'
4
+ import { any } from './validators/any'
3
5
 
4
- import {
5
- AdditionalProperties,
6
- InferValidation,
6
+ import type {
7
+ AdditionalProperties, InferValidation,
7
8
  Validation,
8
9
  Validator,
9
- additionalValidator,
10
10
  } from './types'
11
11
 
12
12
  /* ========================================================================== *
@@ -38,4 +38,6 @@ export function _allowAdditionalProperties(options?: Validation | boolean): Addi
38
38
  export const allowAdditionalProperties = _allowAdditionalProperties as
39
39
  typeof _allowAdditionalProperties & AdditionalProperties<Validator<any>>
40
40
 
41
+ // Remember to inject our `any` validator as the default for when
42
+ // `allowAdditionalProperties` is _not_ used as a function
41
43
  allowAdditionalProperties[additionalValidator] = any