@orion-js/schema 4.0.0-next.7 → 4.0.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/index.cjs +206 -148
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +61 -46
- package/dist/index.d.ts +61 -46
- package/dist/index.js +204 -148
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,3 @@
|
|
|
1
|
-
interface FieldTypeOpts<TType = any> {
|
|
2
|
-
name: string;
|
|
3
|
-
validate?: ValidateFunction<TType>;
|
|
4
|
-
clean?: CleanFunction<TType>;
|
|
5
|
-
toGraphQLType?: (GraphQL: any) => any;
|
|
6
|
-
meta?: any;
|
|
7
|
-
}
|
|
8
|
-
interface FieldType<TType = any> {
|
|
9
|
-
name: string;
|
|
10
|
-
validate: ValidateFunction;
|
|
11
|
-
clean: CleanFunction;
|
|
12
|
-
meta?: any;
|
|
13
|
-
toGraphQLType?: (GraphQL: any) => any;
|
|
14
|
-
__tsFieldType: TType;
|
|
15
|
-
__isFieldType: boolean;
|
|
16
|
-
}
|
|
17
|
-
|
|
18
1
|
type Constructor<T> = new (...args: any[]) => T;
|
|
19
2
|
type Blackbox = {
|
|
20
3
|
[name: string]: any;
|
|
@@ -23,8 +6,10 @@ type FieldTypesList = 'string' | 'date' | 'integer' | 'number' | 'ID' | 'boolean
|
|
|
23
6
|
type AClass$1 = abstract new (...args: any) => any;
|
|
24
7
|
type TypedSchemaOnSchema = AClass$1;
|
|
25
8
|
type ConstructorsTypesList = Constructor<String> | Constructor<Number> | Constructor<Boolean> | Constructor<Date> | StringConstructor | NumberConstructor | BooleanConstructor | DateConstructor | String | Number | Boolean | Date;
|
|
26
|
-
type
|
|
9
|
+
type SchemaMetaFieldTypeSingleNonSchema = FieldTypesList | ConstructorsTypesList | FieldType;
|
|
10
|
+
type SchemaMetaFieldTypeSingle = SchemaMetaFieldTypeSingleNonSchema | Schema | TypedSchemaOnSchema;
|
|
27
11
|
type SchemaFieldType = SchemaMetaFieldTypeSingle | SchemaMetaFieldTypeSingle[];
|
|
12
|
+
type SchemaFieldTypeNonSchema = SchemaMetaFieldTypeSingleNonSchema | SchemaMetaFieldTypeSingleNonSchema[];
|
|
28
13
|
type ValidateFunction<TType = any> = (value: TType, info?: Partial<CurrentNodeInfo>, ...args: any[]) => object | string | null | undefined | void | Promise<object | string | null | undefined | void>;
|
|
29
14
|
type CleanFunction<TType = any> = (value: TType, info?: Partial<CurrentNodeInfo>, ...args: any[]) => TType | Promise<TType>;
|
|
30
15
|
type SchemaRecursiveNodeTypeExtras = {
|
|
@@ -104,22 +89,22 @@ interface CurrentNodeInfoOptions {
|
|
|
104
89
|
forceDoc?: any;
|
|
105
90
|
omitRequired?: boolean;
|
|
106
91
|
}
|
|
107
|
-
interface CurrentNodeInfo
|
|
92
|
+
interface CurrentNodeInfo {
|
|
108
93
|
/**
|
|
109
94
|
* The global schema, prefaced by {type: {...}} to be compatible with subschemas
|
|
110
95
|
* Sometimes it's given without {type: {...}}. TODO: Normalize this.
|
|
111
96
|
*/
|
|
112
|
-
schema?:
|
|
97
|
+
schema?: SchemaFieldType;
|
|
113
98
|
/**
|
|
114
99
|
* The current node subschema
|
|
115
100
|
*/
|
|
116
|
-
currentSchema?: Partial<SchemaNode<
|
|
117
|
-
value:
|
|
101
|
+
currentSchema?: Partial<SchemaNode<SchemaFieldType>>;
|
|
102
|
+
value: InferSchemaType<SchemaFieldType>;
|
|
118
103
|
doc?: any;
|
|
119
104
|
currentDoc?: any;
|
|
120
105
|
options?: CurrentNodeInfoOptions;
|
|
121
106
|
args?: any[];
|
|
122
|
-
type?:
|
|
107
|
+
type?: SchemaFieldType;
|
|
123
108
|
keys?: string[];
|
|
124
109
|
addError?: (keys: string[], code: string | object) => void;
|
|
125
110
|
}
|
|
@@ -140,41 +125,70 @@ type SchemaMetadata = {
|
|
|
140
125
|
type Schema = {
|
|
141
126
|
[K: string]: SchemaNode;
|
|
142
127
|
};
|
|
128
|
+
type SingleLevelSchema = {
|
|
129
|
+
[K: string]: SchemaNode<SchemaFieldTypeNonSchema>;
|
|
130
|
+
};
|
|
131
|
+
type SchemaInAnyOrionForm = Schema | TypedSchemaOnSchema;
|
|
143
132
|
type SchemaWithMetadata = {
|
|
144
133
|
[K: string]: SchemaNode | SchemaMetadata[keyof SchemaMetadata];
|
|
145
134
|
} & SchemaMetadata;
|
|
146
135
|
|
|
136
|
+
interface FieldTypeOpts<TType = any> {
|
|
137
|
+
name: string;
|
|
138
|
+
validate?: ValidateFunction<TType>;
|
|
139
|
+
clean?: CleanFunction<TType>;
|
|
140
|
+
toGraphQLType?: (GraphQL: any) => any;
|
|
141
|
+
meta?: any;
|
|
142
|
+
}
|
|
143
|
+
interface FieldType<TType = any> {
|
|
144
|
+
name: string;
|
|
145
|
+
validate: ValidateFunction;
|
|
146
|
+
clean: CleanFunction;
|
|
147
|
+
meta?: any;
|
|
148
|
+
toGraphQLType?: (GraphQL: any) => any;
|
|
149
|
+
__tsFieldType: TType;
|
|
150
|
+
__isFieldType: boolean;
|
|
151
|
+
}
|
|
152
|
+
|
|
147
153
|
type InferSchemaTypeForFieldType<T> = T extends {
|
|
148
154
|
__tsFieldType: infer U;
|
|
149
155
|
} ? 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 =
|
|
156
|
+
type SchemaKeysNotOfSchemaItems = '__isFieldType' | '__GraphQLType' | '__skipChildValidation';
|
|
151
157
|
type NodeIsOptional<TNode> = TNode extends {
|
|
152
158
|
optional: true;
|
|
153
|
-
} ? true : TNode extends {
|
|
154
|
-
defaultValue: any;
|
|
155
159
|
} ? true : false;
|
|
156
|
-
type
|
|
157
|
-
|
|
160
|
+
type Simplifed<T> = {
|
|
161
|
+
[K in keyof T]: T[K] extends object ? Simplifed<T[K]> : T[K];
|
|
162
|
+
};
|
|
163
|
+
type WithoutNotSchemaItems<T extends Record<string, any>> = T extends {
|
|
164
|
+
[key in SchemaKeysNotOfSchemaItems]: any;
|
|
165
|
+
} & Record<string, any> ? Omit<T, SchemaKeysNotOfSchemaItems> : T;
|
|
166
|
+
type InferSchemaTypeForSchema<TSchema extends Record<string, any>> = Simplifed<WithoutNotSchemaItems<{
|
|
167
|
+
-readonly [K in keyof TSchema as NodeIsOptional<TSchema[K]> extends true ? never : K]: InferSchemaType<TSchema[K]['type']>;
|
|
158
168
|
} & {
|
|
159
|
-
-readonly [K in keyof TSchema as NodeIsOptional<TSchema[K]> extends true ? K : never]?:
|
|
160
|
-
}
|
|
161
|
-
type IsPossiblyASchema<TType> = TType extends Record<string, any> ? keyof {
|
|
169
|
+
-readonly [K in keyof TSchema as NodeIsOptional<TSchema[K]> extends true ? K : never]?: InferSchemaType<TSchema[K]['type']>;
|
|
170
|
+
}>>;
|
|
171
|
+
type IsPossiblyASchema<TType> = TType extends FieldType ? false : TType extends Record<string, any> ? keyof {
|
|
162
172
|
[K in keyof TType as 'type' extends keyof TType[K] ? K : never]: TType[K];
|
|
163
173
|
} extends never ? false : true : false;
|
|
164
|
-
type AClass = abstract new (...args: any) =>
|
|
174
|
+
type AClass<T = any> = abstract new (...args: any) => T;
|
|
165
175
|
/**
|
|
166
176
|
* Returns the type of the schema
|
|
167
177
|
*/
|
|
168
178
|
type InferSchemaType<TType> = TType extends {
|
|
169
179
|
__isModel: true;
|
|
170
180
|
type: infer U;
|
|
171
|
-
} ? InferSchemaTypeForSchema<U> : TType extends
|
|
181
|
+
} ? InferSchemaTypeForSchema<U> : TType extends SchemaMetaFieldTypeSingleNonSchema ? InferSchemaTypeForFieldType<TType> : TType extends AClass<infer U> ? U : IsPossiblyASchema<TType> extends true ? InferSchemaTypeForSchema<TType> : InferSchemaTypeForFieldType<TType>;
|
|
172
182
|
/**
|
|
173
183
|
* Returns the type of the schema but only if its a schema
|
|
174
184
|
*/
|
|
175
185
|
type StrictInferSchemaType<TSchema extends Schema> = InferSchemaTypeForSchema<TSchema>;
|
|
186
|
+
/**
|
|
187
|
+
* Returns the type of the schema but only if its a schema
|
|
188
|
+
*/
|
|
189
|
+
type InferSchemaTypeFromTypedSchema<TTypedSchema extends TypedSchemaOnSchema> = TTypedSchema;
|
|
176
190
|
|
|
177
|
-
declare function validate<TSchema extends
|
|
191
|
+
declare function validate<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>, passedOptions?: {}, ...args: any[]): Promise<void>;
|
|
178
192
|
|
|
179
193
|
interface ValidationErrorInfo {
|
|
180
194
|
error: string;
|
|
@@ -191,10 +205,6 @@ declare class ValidationError extends Error {
|
|
|
191
205
|
prependKey: (prepend: any) => ValidationError;
|
|
192
206
|
}
|
|
193
207
|
|
|
194
|
-
declare function getValidationErrors(schema: Schema | Function, doc: any, passedOptions?: {}, ...args: any[]): Promise<any>;
|
|
195
|
-
|
|
196
|
-
declare function isValid(schema: Schema, doc: any, passedOptions?: {}, ...args: any[]): Promise<boolean>;
|
|
197
|
-
|
|
198
208
|
declare const _default: {
|
|
199
209
|
array: FieldType<any[]>;
|
|
200
210
|
plainObject: FieldType<Blackbox>;
|
|
@@ -211,11 +221,15 @@ declare const _default: {
|
|
|
211
221
|
|
|
212
222
|
type FieldValidatorType = keyof typeof _default | 'custom' | 'plainObject';
|
|
213
223
|
|
|
214
|
-
declare function getFieldType(type: SchemaFieldType | FieldValidatorType | any): FieldType;
|
|
215
|
-
|
|
216
224
|
type MergeSchemas<SchemaA extends Schema, SchemaB extends Schema> = SchemaA & SchemaB;
|
|
217
225
|
|
|
218
|
-
declare function
|
|
226
|
+
declare function getValidationErrors<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>, passedOptions?: {}, ...args: any[]): Promise<any>;
|
|
227
|
+
|
|
228
|
+
declare function isValid<TSchema extends Schema>(schema: TSchema, doc: InferSchemaType<TSchema>, passedOptions?: {}, ...args: any[]): Promise<boolean>;
|
|
229
|
+
|
|
230
|
+
declare function getFieldType(type: SchemaFieldType | FieldValidatorType | any): FieldType;
|
|
231
|
+
|
|
232
|
+
declare function clean<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>, opts?: CurrentNodeInfoOptions, ...args: any[]): Promise<InferSchemaType<TSchema>>;
|
|
219
233
|
|
|
220
234
|
declare function export_default$2(schema: any, key: any, value: any, passedOptions?: {}, ...args: any[]): Promise<any>;
|
|
221
235
|
|
|
@@ -223,16 +237,17 @@ declare function export_default$1(schema: Schema, key: string, value: any, passe
|
|
|
223
237
|
|
|
224
238
|
declare function export_default(schema: Schema, path: string): SchemaNode;
|
|
225
239
|
|
|
226
|
-
declare function createEnum<const TValues extends readonly string[]>(name: string, values: TValues): FieldType<TValues[number]
|
|
227
|
-
type: TValues[number];
|
|
228
|
-
};
|
|
240
|
+
declare function createEnum<const TValues extends readonly string[]>(name: string, values: TValues): FieldType<TValues[number]>;
|
|
229
241
|
|
|
230
242
|
declare function isSchemaLike(type: any): boolean;
|
|
243
|
+
declare function isStrictSchemaLike<TType extends Schema | SchemaFieldTypeNonSchema>(type: TType): TType extends Schema ? true : false;
|
|
231
244
|
declare function isSchemaOrFieldLike(type: any): boolean;
|
|
232
245
|
declare function getSchemaModelName(type: any): string | null;
|
|
233
|
-
declare function getSchemaFromAnyOrionForm(type: any):
|
|
246
|
+
declare function getSchemaFromAnyOrionForm(type: any): SchemaFieldType;
|
|
234
247
|
declare function getSchemaWithMetadataFromAnyOrionForm(type: any): SchemaWithMetadata;
|
|
235
248
|
|
|
249
|
+
declare function cleanAndValidate<TSchema extends SchemaFieldType>(schema: TSchema, doc: InferSchemaType<TSchema>): Promise<InferSchemaType<TSchema>>;
|
|
250
|
+
|
|
236
251
|
/**
|
|
237
252
|
* Assigns a name to a schema for GraphQL type generation.
|
|
238
253
|
*
|
|
@@ -248,4 +263,4 @@ declare function getSchemaWithMetadataFromAnyOrionForm(type: any): SchemaWithMet
|
|
|
248
263
|
*/
|
|
249
264
|
declare function schemaWithName<TModelName extends string, TSchema extends Schema>(name: TModelName, schema: TSchema): TSchema;
|
|
250
265
|
|
|
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 };
|
|
266
|
+
export { type Blackbox, type CleanFunction, type Constructor, type ConstructorsTypesList, type CurrentNodeInfo, type CurrentNodeInfoOptions, type FieldType, type FieldTypeOpts, type FieldTypesList, type FieldValidatorType, type InferSchemaType, type InferSchemaTypeFromTypedSchema, type MergeSchemas, type Schema, type SchemaFieldType, type SchemaFieldTypeNonSchema, type SchemaInAnyOrionForm, type SchemaMetaFieldTypeSingle, type SchemaMetaFieldTypeSingleNonSchema, type SchemaMetadata, type SchemaNode, type SchemaRecursiveNodeTypeExtras, type SchemaWithMetadata, type Simplifed, type SingleLevelSchema, type StrictInferSchemaType, type TypedSchemaOnSchema, type ValidateFunction, ValidationError, clean, cleanAndValidate, export_default$2 as cleanKey, createEnum, export_default as dotGetSchema, _default as fieldTypes, getFieldType, getSchemaFromAnyOrionForm, getSchemaModelName, getSchemaWithMetadataFromAnyOrionForm, getValidationErrors, isSchemaLike, isSchemaOrFieldLike, isStrictSchemaLike, isValid, schemaWithName, validate, export_default$1 as validateKey };
|