@orion-js/schema 4.0.0-next.2 → 4.0.0-next.4
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/LICENSE +21 -0
- package/dist/index.cjs +187 -184
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +116 -55
- package/dist/index.d.ts +116 -55
- package/dist/index.js +180 -186
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,17 +1,18 @@
|
|
|
1
|
-
interface FieldTypeOpts {
|
|
1
|
+
interface FieldTypeOpts<TType = any> {
|
|
2
2
|
name: string;
|
|
3
|
-
validate?: ValidateFunction
|
|
4
|
-
clean?: CleanFunction
|
|
3
|
+
validate?: ValidateFunction<TType>;
|
|
4
|
+
clean?: CleanFunction<TType>;
|
|
5
5
|
toGraphQLType?: (GraphQL: any) => any;
|
|
6
6
|
meta?: any;
|
|
7
7
|
}
|
|
8
|
-
interface FieldType {
|
|
8
|
+
interface FieldType<TType = any> {
|
|
9
9
|
name: string;
|
|
10
10
|
validate: ValidateFunction;
|
|
11
11
|
clean: CleanFunction;
|
|
12
12
|
meta?: any;
|
|
13
13
|
toGraphQLType?: (GraphQL: any) => any;
|
|
14
|
-
|
|
14
|
+
__tsFieldType: TType;
|
|
15
|
+
__isFieldType: boolean;
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
type Constructor<T> = new (...args: any[]) => T;
|
|
@@ -19,42 +20,37 @@ type Blackbox = {
|
|
|
19
20
|
[name: string]: any;
|
|
20
21
|
};
|
|
21
22
|
type FieldTypesList = 'string' | 'date' | 'integer' | 'number' | 'ID' | 'boolean' | 'email' | 'blackbox' | 'any';
|
|
22
|
-
type
|
|
23
|
-
type
|
|
23
|
+
type AClass$1 = abstract new (...args: any) => any;
|
|
24
|
+
type TypedSchemaOnSchema = AClass$1;
|
|
25
|
+
type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date> | StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor | String | Number | Boolean | Date;
|
|
26
|
+
type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | Schema | FieldType | TypedSchemaOnSchema;
|
|
27
|
+
type SchemaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
|
|
28
|
+
type ValidateFunction<TType = any> = (value: TType, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | null | undefined | void | Promise<object | string | null | undefined | void>;
|
|
29
|
+
type CleanFunction<TType = any> = (value: TType, info?: Partial<CurrentNodeInfo>, ...args: any[]) => TType | Promise<TType>;
|
|
24
30
|
type SchemaRecursiveNodeTypeExtras = {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
__validate?: ValidateFunction;
|
|
31
|
+
__isFieldType?: boolean;
|
|
32
|
+
__GraphQLType?: any;
|
|
28
33
|
__skipChildValidation?: (value: any, info: CurrentNodeInfo) => Promise<boolean>;
|
|
29
34
|
};
|
|
30
|
-
|
|
31
|
-
[key: string]: SchemaNode | Function;
|
|
32
|
-
}
|
|
33
|
-
type SchemaRecursiveNodeType = Schema & SchemaRecursiveNodeTypeExtras;
|
|
34
|
-
type SchemaMetaFieldTypeSingle = FieldTypesList | ConstructorsTypesList | SchemaRecursiveNodeType | FieldType | TypedModelOnSchema;
|
|
35
|
-
type SchemaMetaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
|
|
36
|
-
type ValidateFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | void | Promise<object | string | void>;
|
|
37
|
-
type CleanFunction = (value: any, info?: Partial<CurrentNodeInfo>, ...args: any[]) => any | Promise<any>;
|
|
38
|
-
interface SchemaNode {
|
|
35
|
+
type SchemaNode<TFieldType extends SchemaFieldType = SchemaFieldType> = {
|
|
39
36
|
/**
|
|
40
37
|
* The type of the field. Used for type validations. Can also contain a subschema.
|
|
41
38
|
*/
|
|
42
|
-
type:
|
|
39
|
+
type: TFieldType;
|
|
43
40
|
/**
|
|
44
41
|
* Defaults to false
|
|
45
42
|
*/
|
|
46
43
|
optional?: boolean;
|
|
47
|
-
allowedValues?: Array<
|
|
48
|
-
defaultValue?: ((info: CurrentNodeInfo, ...args: any[]) =>
|
|
44
|
+
allowedValues?: Array<InferSchemaType<TFieldType>>;
|
|
45
|
+
defaultValue?: ((info: CurrentNodeInfo, ...args: any[]) => InferSchemaType<TFieldType> | Promise<InferSchemaType<TFieldType>>) | InferSchemaType<TFieldType>;
|
|
49
46
|
/**
|
|
50
47
|
* Function that takes a value and returns an error message if there are any errors. Must return null or undefined otherwise.
|
|
51
48
|
*/
|
|
52
|
-
validate?: ValidateFunction
|
|
49
|
+
validate?: ValidateFunction<InferSchemaType<TFieldType>>;
|
|
53
50
|
/**
|
|
54
51
|
* Function that preprocesses a value before it is set.
|
|
55
52
|
*/
|
|
56
|
-
clean?: CleanFunction
|
|
57
|
-
autoValue?: (value: any, info: CurrentNodeInfo, ...args: any[]) => any | Promise<any>;
|
|
53
|
+
clean?: CleanFunction<InferSchemaType<TFieldType>>;
|
|
58
54
|
/**
|
|
59
55
|
* The minimum value if it's a number, the minimum length if it's a string or array.
|
|
60
56
|
*/
|
|
@@ -67,10 +63,6 @@ interface SchemaNode {
|
|
|
67
63
|
* Internal use only.
|
|
68
64
|
*/
|
|
69
65
|
isBlackboxChild?: boolean;
|
|
70
|
-
/**
|
|
71
|
-
* @deprecated
|
|
72
|
-
*/
|
|
73
|
-
custom?: ValidateFunction;
|
|
74
66
|
/**
|
|
75
67
|
* Used in GraphQL. If true, the field will be omitted from the schema.
|
|
76
68
|
*/
|
|
@@ -103,7 +95,7 @@ interface SchemaNode {
|
|
|
103
95
|
* The field options that will be passed as props to the front-end field
|
|
104
96
|
*/
|
|
105
97
|
fieldOptions?: any;
|
|
106
|
-
}
|
|
98
|
+
} & SchemaRecursiveNodeTypeExtras;
|
|
107
99
|
interface CurrentNodeInfoOptions {
|
|
108
100
|
autoConvert?: boolean;
|
|
109
101
|
filter?: boolean;
|
|
@@ -112,7 +104,7 @@ interface CurrentNodeInfoOptions {
|
|
|
112
104
|
forceDoc?: any;
|
|
113
105
|
omitRequired?: boolean;
|
|
114
106
|
}
|
|
115
|
-
interface CurrentNodeInfo {
|
|
107
|
+
interface CurrentNodeInfo<TType extends SchemaFieldType = any> {
|
|
116
108
|
/**
|
|
117
109
|
* The global schema, prefaced by {type: {...}} to be compatible with subschemas
|
|
118
110
|
* Sometimes it's given without {type: {...}}. TODO: Normalize this.
|
|
@@ -121,18 +113,68 @@ interface CurrentNodeInfo {
|
|
|
121
113
|
/**
|
|
122
114
|
* The current node subschema
|
|
123
115
|
*/
|
|
124
|
-
currentSchema?: Partial<SchemaNode
|
|
125
|
-
value:
|
|
116
|
+
currentSchema?: Partial<SchemaNode<TType>>;
|
|
117
|
+
value: TType;
|
|
126
118
|
doc?: any;
|
|
127
119
|
currentDoc?: any;
|
|
128
120
|
options?: CurrentNodeInfoOptions;
|
|
129
121
|
args?: any[];
|
|
130
|
-
type?:
|
|
122
|
+
type?: TType;
|
|
131
123
|
keys?: string[];
|
|
132
124
|
addError?: (keys: string[], code: string | object) => void;
|
|
133
125
|
}
|
|
126
|
+
type SchemaMetadata = {
|
|
127
|
+
/**
|
|
128
|
+
* The name of the model (to make it compatible with GraphQL)
|
|
129
|
+
*/
|
|
130
|
+
__modelName?: string;
|
|
131
|
+
/**
|
|
132
|
+
* Cleans the whole schema
|
|
133
|
+
*/
|
|
134
|
+
__clean?: CleanFunction;
|
|
135
|
+
/**
|
|
136
|
+
* Validates the whole schema
|
|
137
|
+
*/
|
|
138
|
+
__validate?: ValidateFunction;
|
|
139
|
+
};
|
|
140
|
+
type Schema = {
|
|
141
|
+
[K: string]: SchemaNode;
|
|
142
|
+
};
|
|
143
|
+
type SchemaWithMetadata = {
|
|
144
|
+
[K: string]: SchemaNode | SchemaMetadata[keyof SchemaMetadata];
|
|
145
|
+
} & SchemaMetadata;
|
|
134
146
|
|
|
135
|
-
|
|
147
|
+
type InferSchemaTypeForFieldType<T> = T extends {
|
|
148
|
+
__tsFieldType: infer U;
|
|
149
|
+
} ? U : T extends 'string' ? string : T extends 'date' ? Date : T extends 'integer' ? number : T extends 'number' ? number : T extends 'ID' ? string : T extends 'boolean' ? boolean : T extends 'email' ? string : T extends 'blackbox' ? Blackbox : T extends 'any' ? any : T extends String ? string : T extends Number ? number : T extends Boolean ? boolean : T extends Date ? Date : T extends StringConstructor ? string : T extends NumberConstructor ? number : T extends BooleanConstructor ? boolean : T extends DateConstructor ? Date : T extends Array<infer U> ? InferSchemaTypeForFieldType<U>[] : T extends Record<string, any> ? InferSchemaTypeForSchema<T> : T;
|
|
150
|
+
type SchemaKeysNotOfSchemaItems = keyof SchemaRecursiveNodeTypeExtras;
|
|
151
|
+
type NodeIsOptional<TNode> = TNode extends {
|
|
152
|
+
optional: true;
|
|
153
|
+
} ? true : TNode extends {
|
|
154
|
+
defaultValue: any;
|
|
155
|
+
} ? true : false;
|
|
156
|
+
type InferSchemaTypeForSchema<TSchema extends Record<string, any>> = Omit<{
|
|
157
|
+
-readonly [K in keyof TSchema as NodeIsOptional<TSchema[K]> extends true ? never : K]: InferSchemaTypeForFieldType<TSchema[K]['type']>;
|
|
158
|
+
} & {
|
|
159
|
+
-readonly [K in keyof TSchema as NodeIsOptional<TSchema[K]> extends true ? K : never]?: InferSchemaTypeForFieldType<TSchema[K]['type']>;
|
|
160
|
+
}, SchemaKeysNotOfSchemaItems>;
|
|
161
|
+
type IsPossiblyASchema<TType> = TType extends Record<string, any> ? keyof {
|
|
162
|
+
[K in keyof TType as 'type' extends keyof TType[K] ? K : never]: TType[K];
|
|
163
|
+
} extends never ? false : true : false;
|
|
164
|
+
type AClass = abstract new (...args: any) => any;
|
|
165
|
+
/**
|
|
166
|
+
* Returns the type of the schema
|
|
167
|
+
*/
|
|
168
|
+
type InferSchemaType<TType> = TType extends {
|
|
169
|
+
__isModel: true;
|
|
170
|
+
type: infer U;
|
|
171
|
+
} ? InferSchemaTypeForSchema<U> : TType extends AClass ? InstanceType<TType> : IsPossiblyASchema<TType> extends true ? InferSchemaTypeForSchema<TType> : InferSchemaTypeForFieldType<TType>;
|
|
172
|
+
/**
|
|
173
|
+
* Returns the type of the schema but only if its a schema
|
|
174
|
+
*/
|
|
175
|
+
type StrictInferSchemaType<TSchema extends Schema> = InferSchemaTypeForSchema<TSchema>;
|
|
176
|
+
|
|
177
|
+
declare function validate<TSchema extends Schema = Schema>(schema: TSchema | Function, doc: InferSchemaType<TSchema>, passedOptions?: {}, ...args: any[]): Promise<void>;
|
|
136
178
|
|
|
137
179
|
interface ValidationErrorInfo {
|
|
138
180
|
error: string;
|
|
@@ -154,37 +196,56 @@ declare function getValidationErrors(schema: Schema | Function, doc: any, passed
|
|
|
154
196
|
declare function isValid(schema: Schema, doc: any, passedOptions?: {}, ...args: any[]): Promise<boolean>;
|
|
155
197
|
|
|
156
198
|
declare const _default: {
|
|
157
|
-
array: FieldType
|
|
158
|
-
plainObject: FieldType
|
|
159
|
-
string: FieldType
|
|
160
|
-
date: FieldType
|
|
161
|
-
integer: FieldType
|
|
162
|
-
number: FieldType
|
|
163
|
-
ID: FieldType
|
|
164
|
-
boolean: FieldType
|
|
165
|
-
email: FieldType
|
|
166
|
-
blackbox: FieldType
|
|
167
|
-
any: FieldType
|
|
199
|
+
array: FieldType<any[]>;
|
|
200
|
+
plainObject: FieldType<Blackbox>;
|
|
201
|
+
string: FieldType<string>;
|
|
202
|
+
date: FieldType<Date>;
|
|
203
|
+
integer: FieldType<number>;
|
|
204
|
+
number: FieldType<number>;
|
|
205
|
+
ID: FieldType<string>;
|
|
206
|
+
boolean: FieldType<boolean>;
|
|
207
|
+
email: FieldType<string>;
|
|
208
|
+
blackbox: FieldType<Blackbox>;
|
|
209
|
+
any: FieldType<any>;
|
|
168
210
|
};
|
|
169
211
|
|
|
170
212
|
type FieldValidatorType = keyof typeof _default | 'custom' | 'plainObject';
|
|
171
213
|
|
|
172
|
-
declare function getFieldType(type:
|
|
214
|
+
declare function getFieldType(type: SchemaFieldType | FieldValidatorType | any): FieldType;
|
|
215
|
+
|
|
216
|
+
type MergeSchemas<SchemaA extends Schema, SchemaB extends Schema> = SchemaA & SchemaB;
|
|
173
217
|
|
|
174
|
-
declare function clean<
|
|
218
|
+
declare function clean<TSchema extends Schema = Schema>(schema: TSchema | Function, doc: InferSchemaType<TSchema>, opts?: CurrentNodeInfoOptions, ...args: any[]): Promise<InferSchemaType<TSchema>>;
|
|
175
219
|
|
|
176
220
|
declare function export_default$2(schema: any, key: any, value: any, passedOptions?: {}, ...args: any[]): Promise<any>;
|
|
177
221
|
|
|
178
222
|
declare function export_default$1(schema: Schema, key: string, value: any, passedOptions?: CurrentNodeInfoOptions, ...args: any[]): Promise<any>;
|
|
179
223
|
|
|
180
|
-
declare function export_default(schema: Schema, path: string): SchemaNode
|
|
181
|
-
type: string;
|
|
182
|
-
optional: boolean;
|
|
183
|
-
isBlackboxChild: boolean;
|
|
184
|
-
};
|
|
224
|
+
declare function export_default(schema: Schema, path: string): SchemaNode;
|
|
185
225
|
|
|
186
|
-
declare function createEnum<TValues extends readonly string[]>(name: string, values: TValues): FieldType & {
|
|
226
|
+
declare function createEnum<const TValues extends readonly string[]>(name: string, values: TValues): FieldType<TValues[number]> & {
|
|
187
227
|
type: TValues[number];
|
|
188
228
|
};
|
|
189
229
|
|
|
190
|
-
|
|
230
|
+
declare function isSchemaLike(type: any): boolean;
|
|
231
|
+
declare function isSchemaOrFieldLike(type: any): boolean;
|
|
232
|
+
declare function getSchemaModelName(type: any): string | null;
|
|
233
|
+
declare function getSchemaFromAnyOrionForm(type: any): Schema;
|
|
234
|
+
declare function getSchemaWithMetadataFromAnyOrionForm(type: any): SchemaWithMetadata;
|
|
235
|
+
|
|
236
|
+
/**
|
|
237
|
+
* Assigns a name to a schema for GraphQL type generation.
|
|
238
|
+
*
|
|
239
|
+
* This function associates a name with a schema object by setting an internal
|
|
240
|
+
* `__modelName` property. This name is used when generating GraphQL types.
|
|
241
|
+
*
|
|
242
|
+
* @param name - The name to assign to the schema
|
|
243
|
+
* @param schema - The schema object to name
|
|
244
|
+
* @returns The same schema object with the internal name property added
|
|
245
|
+
*
|
|
246
|
+
* Note: The schema object is modified in-place, so the name will persist
|
|
247
|
+
* even if you don't use the returned value.
|
|
248
|
+
*/
|
|
249
|
+
declare function schemaWithName<TModelName extends string, TSchema extends Schema>(name: TModelName, schema: TSchema): TSchema;
|
|
250
|
+
|
|
251
|
+
export { type Blackbox, type CleanFunction, type Constructor, type ConstructorsTypesList, type CurrentNodeInfo, type CurrentNodeInfoOptions, type FieldType, type FieldTypeOpts, type FieldTypesList, type FieldValidatorType, type InferSchemaType, type MergeSchemas, type Schema, type SchemaFieldType, type SchemaMetaFieldTypeSingle, type SchemaMetadata, type SchemaNode, type SchemaRecursiveNodeTypeExtras, type SchemaWithMetadata, type StrictInferSchemaType, type TypedSchemaOnSchema, type ValidateFunction, ValidationError, clean, export_default$2 as cleanKey, createEnum, export_default as dotGetSchema, _default as fieldTypes, getFieldType, getSchemaFromAnyOrionForm, getSchemaModelName, getSchemaWithMetadataFromAnyOrionForm, getValidationErrors, isSchemaLike, isSchemaOrFieldLike, isValid, schemaWithName, validate, export_default$1 as validateKey };
|