vortez 5.0.0-dev.18 → 5.0.0-dev.19

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 (66) hide show
  1. package/.gitignore +9 -4
  2. package/README.md +681 -176
  3. package/build/Vortez.d.ts +1 -0
  4. package/build/Vortez.js +1 -0
  5. package/build/Vortez.js.map +1 -1
  6. package/build/server/Response.d.ts +1 -1
  7. package/build/server/Response.js +1 -1
  8. package/build/server/Response.js.map +1 -1
  9. package/build/server/Server.d.ts +4 -4
  10. package/build/server/Server.js +5 -5
  11. package/build/server/Server.js.map +1 -1
  12. package/build/server/ServerDebug.d.ts +10 -1
  13. package/build/server/ServerDebug.js +85 -17
  14. package/build/server/ServerDebug.js.map +1 -1
  15. package/build/server/config/Config.d.ts +274 -47
  16. package/build/server/config/Config.js +68 -47
  17. package/build/server/config/Config.js.map +1 -1
  18. package/build/server/config/{ConfigLoader.d.ts → Loader.d.ts} +4 -5
  19. package/build/server/config/{ConfigLoader.js → Loader.js} +7 -10
  20. package/build/server/config/Loader.js.map +1 -0
  21. package/build/server/router/Router.d.ts +87 -30
  22. package/build/server/router/Router.js +110 -48
  23. package/build/server/router/Router.js.map +1 -1
  24. package/build/server/router/algorithm/Algorithm.d.ts +39 -0
  25. package/build/server/router/algorithm/Algorithm.js +20 -0
  26. package/build/server/router/algorithm/Algorithm.js.map +1 -0
  27. package/build/server/router/algorithm/FIFO.d.ts +15 -0
  28. package/build/server/router/algorithm/FIFO.js +24 -0
  29. package/build/server/router/algorithm/FIFO.js.map +1 -0
  30. package/build/server/router/algorithm/Tree.d.ts +38 -0
  31. package/build/server/router/algorithm/Tree.js +126 -0
  32. package/build/server/router/algorithm/Tree.js.map +1 -0
  33. package/build/server/router/middleware/WsMiddleware.js +1 -1
  34. package/build/server/router/middleware/WsMiddleware.js.map +1 -1
  35. package/build/utilities/Flatten.d.ts +56 -0
  36. package/build/utilities/Flatten.js +59 -0
  37. package/build/utilities/Flatten.js.map +1 -0
  38. package/build/utilities/Utilities.d.ts +7 -58
  39. package/build/utilities/Utilities.js +8 -33
  40. package/build/utilities/Utilities.js.map +1 -1
  41. package/build/utilities/schema/Introspection.d.ts +24 -0
  42. package/build/utilities/schema/Introspection.js +87 -0
  43. package/build/utilities/schema/Introspection.js.map +1 -0
  44. package/build/utilities/schema/JSONSchema.d.ts +68 -0
  45. package/build/utilities/schema/JSONSchema.js +13 -0
  46. package/build/utilities/schema/JSONSchema.js.map +1 -0
  47. package/build/utilities/schema/Schema.d.ts +253 -0
  48. package/build/utilities/schema/Schema.js +241 -0
  49. package/build/utilities/schema/Schema.js.map +1 -0
  50. package/build/utilities/schema/SchemaError.d.ts +10 -0
  51. package/build/utilities/schema/SchemaError.js +13 -0
  52. package/build/utilities/schema/SchemaError.js.map +1 -0
  53. package/build/utilities/schema/Validator.d.ts +94 -0
  54. package/build/utilities/schema/Validator.js +246 -0
  55. package/build/utilities/schema/Validator.js.map +1 -0
  56. package/package.json +1 -1
  57. package/tests/config/config.js +233 -0
  58. package/tests/router.js +596 -0
  59. package/tests/schema/schema.js +368 -0
  60. package/tests/test.env +0 -0
  61. package/tests/test.js +3 -3
  62. package/build/server/config/ConfigLoader.js.map +0 -1
  63. package/build/server/config/ConfigValidator.d.ts +0 -71
  64. package/build/server/config/ConfigValidator.js +0 -131
  65. package/build/server/config/ConfigValidator.js.map +0 -1
  66. package/examples/in-docs.js +0 -96
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Provides type definitions for JSON Schema, a powerful tool for validating and describing the structure of JSON data.
4
+ * @license Apache-2.0
5
+ */
6
+ /**
7
+ * provide the type definition of a json schema
8
+ * @param schema the json schema to validate
9
+ */
10
+ export function schema(schema) { return schema; }
11
+ export const JSONSchema = { schema };
12
+ export default JSONSchema;
13
+ //# sourceMappingURL=JSONSchema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"JSONSchema.js","sourceRoot":"","sources":["../../../src/utilities/schema/JSONSchema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAoEH;;;GAGG;AACH,MAAM,UAAU,MAAM,CAAC,MAAyB,IAAI,OAAO,MAAM,CAAC,CAAC,CAAC;AACpE,MAAM,CAAC,MAAM,UAAU,GAAG,EAAE,MAAM,EAAE,CAAC;AACrC,eAAe,UAAU,CAAC"}
@@ -0,0 +1,253 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Provides a comprehensive schema validation and processing system, allowing developers to define complex data structures.
4
+ * @license Apache-2.0
5
+ */
6
+ import type Utilities from '../Utilities.js';
7
+ import _SchemaError from './SchemaError.js';
8
+ import _JSONSchema from './JSONSchema.js';
9
+ import _Introspection from './Introspection.js';
10
+ import _Validator from './Validator.js';
11
+ export { SchemaError } from './SchemaError.js';
12
+ export { JSONSchema } from './JSONSchema.js';
13
+ export { Introspection } from './Introspection.js';
14
+ export { Validator } from './Validator.js';
15
+ export declare class Schema<const S extends Schema.Schema> {
16
+ readonly schema: S;
17
+ constructor(schema: S);
18
+ /**
19
+ * Get the inferred type of the schema.
20
+ *
21
+ * ⚠️ IMPORTANT: This getter returns an EMPTY object. It is designed ONLY for type inference.
22
+ *
23
+ * Usage: Use with `typeof` to extract the inferred type:
24
+ * ```typescript
25
+ * type infered = typeof schemaInstance.infer;
26
+ * ```
27
+ *
28
+ * DO NOT use the returned value at runtime - it's always an empty object.
29
+ * This is purely a TypeScript type utility.
30
+ *
31
+ * @returns An empty object with the inferred type
32
+ */
33
+ get infer(): Schema.Infer<this['schema']>;
34
+ /**
35
+ * Get the inferred type of the schema for processing (i.e., before applying defaults and handling optional properties).
36
+ *
37
+ * ⚠️ IMPORTANT: This getter returns an EMPTY object. It is designed ONLY for type inference.
38
+ *
39
+ * Usage: Use with `typeof` to extract the inferred type for processing:
40
+ * ```typescript
41
+ * type inferedToProcess = typeof schemaInstance.inferToProcess;
42
+ * ```
43
+ *
44
+ * DO NOT use the returned value at runtime - it's always an empty object.
45
+ * This is purely a TypeScript type utility.
46
+ */
47
+ get inferToProcess(): Schema.InferToProcess<this['schema']>;
48
+ /**
49
+ * get the json schema as an object
50
+ * @returns the json schema
51
+ */
52
+ get jsonSchema(): Schema.JSONSchema.schema;
53
+ /**
54
+ * get the json schema as a JSON string
55
+ * @returns the json schema as a string
56
+ */
57
+ get jsonSchemaJSON(): string;
58
+ /**
59
+ * get the list of unique keys
60
+ * @returns the list of unique keys
61
+ */
62
+ get uniques(): string[];
63
+ /**
64
+ * process the provided data
65
+ * @param data the data to process
66
+ * @param partial if the data is partial
67
+ * @returns the processed data
68
+ * @throws schemaError if the data is not valid
69
+ */
70
+ processData(data: Schema.Infer<this['schema']>, partial?: boolean): Schema.Infer<this['schema']>;
71
+ processData(data: Schema.InferToProcess<this['schema']>, partial?: boolean): Schema.Infer<this['schema']>;
72
+ /**
73
+ * process the provided data as partial, meaning that it will only validate the provided properties and ignore the rest.
74
+ * this is useful for validating data that is only meant to update a document, where only a subset of the properties are provided.
75
+ * @param data the data to process
76
+ * @returns the processed data
77
+ * @throws schemaError if the data is not valid
78
+ */
79
+ processPartialData(data: Partial<Schema.FlattenToProcess<this['schema']>> & Partial<Schema.InferToProcess<this['schema']>> & Schema.Document): Partial<Schema.Flatten<this['schema']>> & Partial<Schema.Infer<this['schema']>> & Schema.Document;
80
+ processPartialData(data: Partial<Schema.Flatten<this['schema']>> & Partial<Schema.Infer<this['schema']>> & Schema.Document): Partial<Schema.Flatten<this['schema']>> & Partial<Schema.Infer<this['schema']>> & Schema.Document;
81
+ /**
82
+ * process a property
83
+ * @param data the data to process
84
+ * @param prop the property to process
85
+ * @param key the key of the property
86
+ * @param partial if the data is partial
87
+ * @returns the processed data
88
+ * @throws schemaError if the data is not valid
89
+ */
90
+ protected processProperty(data: any, prop: Schema.property, key: string, partial?: boolean): any;
91
+ /**
92
+ * validate a array
93
+ * @param value the value to validate
94
+ * @param prop the property to validate
95
+ * @param key the key of the property
96
+ * @returns the data
97
+ * @throws schemaError if the data is not valid
98
+ */
99
+ protected processArray(value: any[], prop: Schema.Property.Array, key: string): any;
100
+ /**
101
+ * validate a object
102
+ * @param value the value to validate
103
+ * @param prop the property to validate
104
+ * @param key the key of the property
105
+ * @returns the data
106
+ * @throws schemaError if the data is not valid
107
+ */
108
+ protected processObject(value: any, prop: Schema.Property.Object, key: string, partial?: boolean): any;
109
+ /**
110
+ * validate a array
111
+ * @param value the value to validate
112
+ * @param prop the property to validate
113
+ * @param key the key of the property
114
+ * @throws schemaError if the data is not valid
115
+ */
116
+ protected validateArray(value: any, prop: Schema.Property.Array, key: string): void;
117
+ /**
118
+ * generate a list of unique keys
119
+ * @param doc the schema to validate
120
+ * @param parentKey the parent key of the schema
121
+ * @returns a list of unique keys
122
+ */
123
+ protected listUniques(doc?: Schema.Schema, parentKey?: string): string[];
124
+ /**
125
+ * convert a schema to a JSON schema
126
+ * @param schema the schema to convert
127
+ * @returns the JSON schema
128
+ */
129
+ protected toJsonSchema(schema?: Schema.Schema): Schema.JSONSchema.schema;
130
+ /**
131
+ * -- TYPE GUARD --
132
+ * verify if the key is in the schema
133
+ * @param doc the object to verify
134
+ * @param key the key to verify
135
+ * @returns true if the key is in the schema
136
+ */
137
+ private isKeyOf;
138
+ }
139
+ export declare namespace Schema {
140
+ export import SchemaError = _SchemaError;
141
+ export import JSONSchema = _JSONSchema;
142
+ export import Introspection = _Introspection;
143
+ export import Validator = _Validator;
144
+ type Infer<S extends Schema> = Schema.Infer.schema<S>;
145
+ type InferToProcess<S extends Schema> = Schema.Infer.schemaToProcess<S>;
146
+ type Flatten<S extends Schema.Schema> = (Utilities.Flatten.Object<Infer.schema<S>, 10>);
147
+ type FlattenToProcess<S extends Schema.Schema> = (Utilities.Flatten.Object<Infer.schemaToProcess<S>, 10>);
148
+ interface Document {
149
+ [Key: string]: any;
150
+ }
151
+ interface TypeMap {
152
+ string: string;
153
+ number: number;
154
+ boolean: boolean;
155
+ object: any;
156
+ array: any[];
157
+ }
158
+ namespace Helper {
159
+ type HasDefault<T> = T extends {
160
+ default: any;
161
+ } ? true : false;
162
+ type IsRequired<T> = T extends {
163
+ required: true;
164
+ } ? true : false;
165
+ type IsNullable<T> = T extends {
166
+ nullable: true;
167
+ } ? true : false;
168
+ type DefaultValue<T> = T extends {
169
+ default: infer D;
170
+ } ? D : never;
171
+ type Prettify<T> = {
172
+ [K in keyof T]: T[K];
173
+ } & {};
174
+ }
175
+ namespace Property {
176
+ interface Base<T extends keyof TypeMap> {
177
+ type: T;
178
+ required?: boolean;
179
+ nullable?: boolean;
180
+ unique?: boolean;
181
+ default?: TypeMap[T] | null;
182
+ }
183
+ export interface String extends Base<'string'> {
184
+ enum?: readonly string[];
185
+ pattern?: RegExp;
186
+ minLength?: number;
187
+ maxLength?: number;
188
+ }
189
+ export interface Number extends Base<'number'> {
190
+ minimum?: number;
191
+ maximum?: number;
192
+ }
193
+ export interface Boolean extends Base<'boolean'> {
194
+ }
195
+ export interface Object extends Base<'object'> {
196
+ schema: Schema;
197
+ }
198
+ export interface Array extends Base<'array'> {
199
+ property: property;
200
+ minimum?: number;
201
+ maximum?: number;
202
+ }
203
+ export interface Map {
204
+ string: String;
205
+ number: Number;
206
+ boolean: Boolean;
207
+ object: Object;
208
+ array: Array;
209
+ }
210
+ export {};
211
+ }
212
+ type property = Property.Map[keyof Property.Map];
213
+ interface Schema {
214
+ [Key: string]: property;
215
+ }
216
+ namespace Infer {
217
+ export type Mode = 'partial' | 'process' | 'complete';
218
+ type ObjectByMode<S extends Schema.Schema, M extends Mode> = (M extends 'process' ? schemaToProcess<S> : M extends 'partial' ? schemaPartial<S> : schema<S>);
219
+ export type propertyType<P extends Schema.property, M extends Mode = 'complete'> = (P extends Property.String ? P extends {
220
+ enum: readonly (infer E extends string)[];
221
+ } ? E : string : P extends Property.Number ? number : P extends Property.Boolean ? boolean : P extends Property.Object ? (ObjectByMode<P['schema'], M>) : P extends Property.Array ? propertyType<P['property'], M>[] : never);
222
+ type OptionalPropertyValue<P extends Schema.property, M extends Mode> = (Helper.IsNullable<P> extends true ? propertyType<P, M> | null : propertyType<P, M> | undefined);
223
+ type DefaultedPropertyValue<P extends Schema.property, M extends Mode> = (Helper.DefaultValue<P> extends null ? propertyType<P, M> | null : propertyType<P, M>);
224
+ type RequiredPropertyValue<P extends Schema.property, M extends Mode> = (Helper.IsNullable<P> extends true ? propertyType<P, M> | null : propertyType<P, M>);
225
+ export type property<P extends Schema.property, M extends Mode = 'complete'> = (Helper.HasDefault<P> extends true ? DefaultedPropertyValue<P, M> : Helper.IsRequired<P> extends true ? RequiredPropertyValue<P, M> : OptionalPropertyValue<P, M>);
226
+ type RequiredKeys<S extends Schema> = {
227
+ [K in keyof S]: Helper.IsRequired<S[K]> extends true ? K : Helper.HasDefault<S[K]> extends true ? K : never;
228
+ }[keyof S];
229
+ type OptionalKeys<S extends Schema> = Exclude<keyof S, RequiredKeys<S>>;
230
+ type RequiredToProcessKeys<S extends Schema> = {
231
+ [K in keyof S]: Helper.IsRequired<S[K]> extends true ? Helper.HasDefault<S[K]> extends false ? K : never : never;
232
+ }[keyof S];
233
+ type OptionalToProcessKeys<S extends Schema> = Exclude<keyof S, RequiredToProcessKeys<S>>;
234
+ export type schema<S extends Schema> = Helper.Prettify<{
235
+ [K in RequiredKeys<S>]: property<S[K]>;
236
+ } & {
237
+ [K in OptionalKeys<S>]?: property<S[K]>;
238
+ }>;
239
+ export type schemaToProcess<S extends Schema> = Helper.Prettify<{
240
+ [K in RequiredToProcessKeys<S>]: property<S[K], 'process'>;
241
+ } & {
242
+ [K in OptionalToProcessKeys<S>]?: property<S[K], 'process'>;
243
+ }>;
244
+ export type schemaPartial<S extends Schema> = Helper.Prettify<{
245
+ [K in keyof S]?: property<S[K], 'partial'>;
246
+ }>;
247
+ export type schemaBase<S extends Schema> = {
248
+ [K in keyof S]: property<S[K]>;
249
+ };
250
+ export {};
251
+ }
252
+ }
253
+ export default Schema;
@@ -0,0 +1,241 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Provides a comprehensive schema validation and processing system, allowing developers to define complex data structures.
4
+ * @license Apache-2.0
5
+ */
6
+ import _SchemaError from './SchemaError.js';
7
+ import _JSONSchema from './JSONSchema.js';
8
+ import _Introspection from './Introspection.js';
9
+ import _Validator from './Validator.js';
10
+ export { SchemaError } from './SchemaError.js';
11
+ export { JSONSchema } from './JSONSchema.js';
12
+ export { Introspection } from './Introspection.js';
13
+ export { Validator } from './Validator.js';
14
+ export class Schema {
15
+ schema;
16
+ constructor(schema) {
17
+ this.schema = schema;
18
+ Schema.Validator.validateStructure(schema);
19
+ }
20
+ /**
21
+ * Get the inferred type of the schema.
22
+ *
23
+ * ⚠️ IMPORTANT: This getter returns an EMPTY object. It is designed ONLY for type inference.
24
+ *
25
+ * Usage: Use with `typeof` to extract the inferred type:
26
+ * ```typescript
27
+ * type infered = typeof schemaInstance.infer;
28
+ * ```
29
+ *
30
+ * DO NOT use the returned value at runtime - it's always an empty object.
31
+ * This is purely a TypeScript type utility.
32
+ *
33
+ * @returns An empty object with the inferred type
34
+ */
35
+ get infer() {
36
+ return {};
37
+ }
38
+ /**
39
+ * Get the inferred type of the schema for processing (i.e., before applying defaults and handling optional properties).
40
+ *
41
+ * ⚠️ IMPORTANT: This getter returns an EMPTY object. It is designed ONLY for type inference.
42
+ *
43
+ * Usage: Use with `typeof` to extract the inferred type for processing:
44
+ * ```typescript
45
+ * type inferedToProcess = typeof schemaInstance.inferToProcess;
46
+ * ```
47
+ *
48
+ * DO NOT use the returned value at runtime - it's always an empty object.
49
+ * This is purely a TypeScript type utility.
50
+ */
51
+ get inferToProcess() {
52
+ return {};
53
+ }
54
+ /**
55
+ * get the json schema as an object
56
+ * @returns the json schema
57
+ */
58
+ get jsonSchema() { return this.toJsonSchema(); }
59
+ /**
60
+ * get the json schema as a JSON string
61
+ * @returns the json schema as a string
62
+ */
63
+ get jsonSchemaJSON() { return JSON.stringify(this.jsonSchema); }
64
+ /**
65
+ * get the list of unique keys
66
+ * @returns the list of unique keys
67
+ */
68
+ get uniques() { return this.listUniques(); }
69
+ processData(data, partial = false) {
70
+ const result = {};
71
+ const iterable = partial ? data : this.schema;
72
+ for (const key in iterable) {
73
+ if (!this.isKeyOf(this.schema, key))
74
+ throw new Schema.SchemaError(`Unknown property ${String(key)}`);
75
+ const prop = this.schema[key];
76
+ const value = this.isKeyOf(data, key) ? data[key] : undefined;
77
+ result[key] = this.processProperty(value, prop, key, partial);
78
+ if (result[key] === undefined)
79
+ delete result[key];
80
+ }
81
+ return result;
82
+ }
83
+ processPartialData(data) {
84
+ const result = {};
85
+ let currentProp;
86
+ for (const key in data) {
87
+ const value = this.isKeyOf(data, key) ? data[key] : undefined;
88
+ const subKeys = key.split('.');
89
+ const firstKey = subKeys.shift();
90
+ if (!firstKey || !(firstKey in this.schema))
91
+ throw new Schema.SchemaError(`Unknown property ${firstKey}`);
92
+ currentProp = this.schema[firstKey];
93
+ if (subKeys.length === 0)
94
+ result[key] = this.processProperty(value, currentProp, key);
95
+ else {
96
+ if (currentProp.type !== 'object')
97
+ throw new Schema.SchemaError(`Property ${key} is not an object`);
98
+ let objectProp = currentProp;
99
+ let usedKeys = [];
100
+ for (const subKey of subKeys) {
101
+ usedKeys.push(subKey);
102
+ if (!(subKey in objectProp.schema))
103
+ throw new Schema.SchemaError(`Unknown property ${firstKey}.${usedKeys.join('.')}`);
104
+ currentProp = objectProp.schema[subKey];
105
+ if (currentProp.type === 'object')
106
+ objectProp = currentProp;
107
+ }
108
+ result[key] = this.processProperty(value, currentProp, key);
109
+ }
110
+ if (result[key] === undefined)
111
+ delete result[key];
112
+ }
113
+ return result;
114
+ }
115
+ /**
116
+ * process a property
117
+ * @param data the data to process
118
+ * @param prop the property to process
119
+ * @param key the key of the property
120
+ * @param partial if the data is partial
121
+ * @returns the processed data
122
+ * @throws schemaError if the data is not valid
123
+ */
124
+ processProperty(data, prop, key, partial = false) {
125
+ if (data === undefined || data === null) {
126
+ if ('default' in prop)
127
+ return prop.default;
128
+ if (prop.nullable && prop.nullable === true)
129
+ return null;
130
+ if (prop.required && prop.required === true)
131
+ throw new Schema.SchemaError(`Property ${key} is required but not provided`);
132
+ else
133
+ return undefined;
134
+ }
135
+ switch (prop.type) {
136
+ case 'string':
137
+ Schema.Validator.validateString(data, prop, key);
138
+ return data;
139
+ case 'number':
140
+ Schema.Validator.validateNumber(data, prop, key);
141
+ return data;
142
+ case 'boolean':
143
+ Schema.Validator.validateBoolean(data, prop, key);
144
+ return data;
145
+ case 'array':
146
+ Schema.Validator.validateArray(data, prop, key);
147
+ return this.processArray(data, prop, key);
148
+ case 'object':
149
+ Schema.Validator.validateObject(data, prop, key);
150
+ return this.processObject(data, prop, key, partial);
151
+ default: throw new Schema.SchemaError(`Unknown type in property ${key}`);
152
+ }
153
+ }
154
+ /**
155
+ * validate a array
156
+ * @param value the value to validate
157
+ * @param prop the property to validate
158
+ * @param key the key of the property
159
+ * @returns the data
160
+ * @throws schemaError if the data is not valid
161
+ */
162
+ processArray(value, prop, key) {
163
+ try {
164
+ return value.map((item, index) => this.processProperty(item, prop.property, `${key}[${index}]`));
165
+ }
166
+ catch (error) {
167
+ throw new Schema.SchemaError(`Property ${key} is not valid: ${error}`);
168
+ }
169
+ }
170
+ /**
171
+ * validate a object
172
+ * @param value the value to validate
173
+ * @param prop the property to validate
174
+ * @param key the key of the property
175
+ * @returns the data
176
+ * @throws schemaError if the data is not valid
177
+ */
178
+ processObject(value, prop, key, partial = false) {
179
+ const handler = new Schema(prop.schema);
180
+ try {
181
+ return handler.processData(value, partial);
182
+ }
183
+ catch (error) {
184
+ throw new Schema.SchemaError(`Property ${key} is not valid: ${error}`);
185
+ }
186
+ }
187
+ /**
188
+ * validate a array
189
+ * @param value the value to validate
190
+ * @param prop the property to validate
191
+ * @param key the key of the property
192
+ * @throws schemaError if the data is not valid
193
+ */
194
+ validateArray(value, prop, key) {
195
+ if (value == null && prop.nullable === true)
196
+ return;
197
+ if (!Array.isArray(value))
198
+ throw new Schema.SchemaError(`Property ${key} must be an array`);
199
+ if (prop.minimum !== undefined && value.length < prop.minimum) {
200
+ throw new Schema.SchemaError(`Property ${key} must have at least ${prop.minimum} items`);
201
+ }
202
+ if (prop.maximum !== undefined && value.length > prop.maximum) {
203
+ throw new Schema.SchemaError(`Property ${key} must have at most ${prop.maximum} items`);
204
+ }
205
+ }
206
+ /**
207
+ * generate a list of unique keys
208
+ * @param doc the schema to validate
209
+ * @param parentKey the parent key of the schema
210
+ * @returns a list of unique keys
211
+ */
212
+ listUniques(doc, parentKey) {
213
+ const useDoc = doc ?? this.schema;
214
+ return Schema.Introspection.listUniques(useDoc, parentKey);
215
+ }
216
+ /**
217
+ * convert a schema to a JSON schema
218
+ * @param schema the schema to convert
219
+ * @returns the JSON schema
220
+ */
221
+ toJsonSchema(schema) {
222
+ const useSchema = schema ?? this.schema;
223
+ return Schema.Introspection.toJsonSchema(useSchema);
224
+ }
225
+ /**
226
+ * -- TYPE GUARD --
227
+ * verify if the key is in the schema
228
+ * @param doc the object to verify
229
+ * @param key the key to verify
230
+ * @returns true if the key is in the schema
231
+ */
232
+ isKeyOf(doc, key) { return key in doc; }
233
+ }
234
+ (function (Schema) {
235
+ Schema.SchemaError = _SchemaError;
236
+ Schema.JSONSchema = _JSONSchema;
237
+ Schema.Introspection = _Introspection;
238
+ Schema.Validator = _Validator;
239
+ })(Schema || (Schema = {}));
240
+ export default Schema;
241
+ //# sourceMappingURL=Schema.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Schema.js","sourceRoot":"","sources":["../../../src/utilities/schema/Schema.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,YAAY,MAAM,kBAAkB,CAAC;AAC5C,OAAO,WAAW,MAAM,iBAAiB,CAAC;AAC1C,OAAO,cAAc,MAAM,oBAAoB,CAAC;AAChD,OAAO,UAAU,MAAM,gBAAgB,CAAC;AAExC,OAAO,EAAE,WAAW,EAAE,MAAM,kBAAkB,CAAC;A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C,WAAW,CAAC,YAAY,GAAG,sBAAsB,IAAI,CAAC,OAAO,QAAQ,CAAC,CAAC;QAC5F,CAAC;IACL,CAAC;IACD;;;;;MAKE;IACQ,WAAW,CAAC,GAAmB,EAAE,SAAkB;QACzD,MAAM,MAAM,GAAG,GAAG,IAAI,IAAI,CAAC,MAAM,CAAC;QAClC,OAAO,MAAM,CAAC,aAAa,CAAC,WAAW,CAAC,MAAM,EAAE,SAAS,CAAC,CAAC;IAC/D,CAAC;IACD;;;;MAIE;IACQ,YAAY,CAAC,MAAsB;QACzC,MAAM,SAAS,GAAG,MAAM,IAAI,IAAI,CAAC,MAAM,CAAC;QACxC,OAAO,MAAM,CAAC,aAAa,CAAC,YAAY,CAAC,SAAS,CAAC,CAAC;IACxD,CAAC;IACD;;;;;;OAMG;IACK,OAAO,CACX,GAAM,EACN,GAAQ,IACQ,OAAO,GAAG,IAAI,GAAG,CAAC,CAAC,CAAC;CAC3C;AAED,WAAiB,MAAM;IACL,kBAAW,GAAG,YAAY,CAAC;IAC3B,iBAAU,GAAG,WAAW,CAAC;IACzB,oBAAa,GAAG,cAAc,CAAC;IAC/B,gBAAS,GAAG,UAAU,CAAC;AAwKzC,CAAC,EA5KgB,MAAM,KAAN,MAAM,QA4KtB;AACD,eAAe,MAAM,CAAC"}
@@ -0,0 +1,10 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Defines a custom error class for handling schema-related errors in the schema validation and processing system.
4
+ * @license Apache-2.0
5
+ */
6
+ export declare class SchemaError extends Error {
7
+ constructor(message: string);
8
+ }
9
+ export declare namespace SchemaError { }
10
+ export default SchemaError;
@@ -0,0 +1,13 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Defines a custom error class for handling schema-related errors in the schema validation and processing system.
4
+ * @license Apache-2.0
5
+ */
6
+ export class SchemaError extends Error {
7
+ constructor(message) {
8
+ super(message);
9
+ this.name = 'schemaError';
10
+ }
11
+ }
12
+ export default SchemaError;
13
+ //# sourceMappingURL=SchemaError.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SchemaError.js","sourceRoot":"","sources":["../../../src/utilities/schema/SchemaError.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,MAAM,OAAO,WAAY,SAAQ,KAAK;IAClC,YAAY,OAAe;QACvB,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,IAAI,GAAG,aAAa,CAAC;IAC9B,CAAC;CACJ;AAED,eAAe,WAAW,CAAC"}
@@ -0,0 +1,94 @@
1
+ /**
2
+ * @author NetFeez <netfeez.dev@gmail.com>
3
+ * @description Provides utilities for validating the structure of a schema, ensuring that property definitions are consistent and valid according to their types and constraints.
4
+ * @license Apache-2.0
5
+ */
6
+ import type { Schema } from './Schema.js';
7
+ export declare class Validator {
8
+ /**
9
+ * Validates the structure of a schema, used for validating the structure of a schema and nested objects.
10
+ * @param schema The schema to validate
11
+ * @param parentKey The parent key of the schema, used for error messages
12
+ */
13
+ static validateStructure(schema: Schema.Schema, parentKey?: string): void;
14
+ /**
15
+ * Validates a property definition, used for validating the structure of a schema and nested objects.
16
+ * @param prop The property definition to validate
17
+ * @param key The key of the property, used for error messages
18
+ */
19
+ static validateProperty(prop: Schema.property, key: string): void;
20
+ /**
21
+ * Validates a default value against a property definition, used for validating default values.
22
+ * @param prop The property definition to validate against
23
+ * @param key The key of the property, used for error messages
24
+ */
25
+ static validateDefaultValue(prop: Schema.property, key: string): void;
26
+ /**
27
+ * Validates a string property definition, used for validating nested objects.
28
+ * @param prop The string property definition to validate
29
+ * @param key The key of the property, used for error messages
30
+ */
31
+ static validateStringProperty(prop: Schema.Property.String, key: string): void;
32
+ /**
33
+ * Validates a number property definition, used for validating nested objects.
34
+ * @param prop The number property definition to validate
35
+ * @param key The key of the property, used for error messages
36
+ */
37
+ static validateNumberProperty(prop: Schema.Property.Number, key: string): void;
38
+ /**
39
+ * Validates an array property definition, used for validating nested arrays.
40
+ * @param prop The array property definition to validate
41
+ * @param key The key of the property, used for error messages
42
+ */
43
+ static validateArrayProperty(prop: Schema.Property.Array, key: string): void;
44
+ /**
45
+ * Validates an object property definition, used for validating nested objects.
46
+ * @param prop The object property definition to validate
47
+ * @param key The key of the property, used for error messages
48
+ */
49
+ static validateObjectProperty(prop: Schema.Property.Object, key: string): void;
50
+ /**
51
+ * Validates a string value against a string property definition, used for validating default values and nested objects.
52
+ * @param value The value to validate
53
+ * @param prop The string property definition to validate against
54
+ * @param key The key of the property, used for error messages
55
+ */
56
+ static validateString(value: string, prop: Schema.Property.String, key: string): void;
57
+ /**
58
+ * Validates a number value against a number property definition, used for validating default values.
59
+ * @param value The value to validate
60
+ * @param prop The number property definition to validate against
61
+ * @param key The key of the property, used for error messages
62
+ */
63
+ static validateNumber(value: number, prop: Schema.Property.Number, key: string): void;
64
+ /**
65
+ * Validates a boolean value against a boolean property definition, used for validating default values.
66
+ * @param value The value to validate
67
+ * @param prop The boolean property definition to validate against
68
+ * @param key The key of the property, used for error messages
69
+ */
70
+ static validateBoolean(value: boolean, prop: Schema.Property.Boolean, key: string): void;
71
+ /**
72
+ * Validates an object value against an object property definition, used for validating default values and nested objects.
73
+ * @param value The value to validate
74
+ * @param prop The object property definition to validate against
75
+ * @param key The key of the property, used for error messages
76
+ */
77
+ static validateObject(value: any, prop: Schema.Property.Object, key: string): void;
78
+ /**
79
+ * Validates an array value against an array property definition, used for validating default values and array items.
80
+ * @param value The value to validate
81
+ * @param prop The array property definition to validate against
82
+ * @param key The key of the property, used for error messages
83
+ */
84
+ static validateArray(value: any, prop: Schema.Property.Array, key: string): void;
85
+ /**
86
+ * Validates a value against a property definition, used for validating default values and array items.
87
+ * @param value The value to validate
88
+ * @param prop The property definition to validate against
89
+ * @param key The key of the property, used for error messages
90
+ */
91
+ static validateValue(value: any, prop: Schema.property, key: string): void;
92
+ }
93
+ export declare namespace Validator { }
94
+ export default Validator;