@orion-js/schema 4.0.0-next.3 → 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/dist/index.cjs +138 -68
- 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 +131 -68
- 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 };
|
package/dist/index.js
CHANGED
|
@@ -41,36 +41,13 @@ var ValidationError = class _ValidationError extends Error {
|
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
43
|
|
|
44
|
-
// src/
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
if (obj.prototype === void 0) {
|
|
48
|
-
return isCtorClass;
|
|
49
|
-
}
|
|
50
|
-
const isPrototypeCtorClass = obj.prototype.constructor && obj.prototype.constructor.toString && obj.prototype.constructor.toString().substring(0, 5) === "class";
|
|
51
|
-
return isCtorClass || isPrototypeCtorClass;
|
|
52
|
-
}
|
|
53
|
-
var convertOnParam = (info, paramName) => {
|
|
54
|
-
if (!info[paramName]) return;
|
|
55
|
-
const type = info[paramName].type;
|
|
56
|
-
if (!type) return;
|
|
57
|
-
if (typeof type !== "function") return;
|
|
58
|
-
if (!isClass(type)) return;
|
|
59
|
-
if (!type.getModel || !type.__schemaId) return;
|
|
60
|
-
info[paramName].type = type.getModel().getCleanSchema();
|
|
61
|
-
};
|
|
62
|
-
var convertTypedModel = (info) => {
|
|
63
|
-
convertOnParam(info, "schema");
|
|
64
|
-
convertOnParam(info, "currentSchema");
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// src/getSchemaFromTypedModel.ts
|
|
68
|
-
var getSchemaFromTypedModel = (schema) => {
|
|
44
|
+
// src/getSchemaFromTypedSchema.ts
|
|
45
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
46
|
+
var getSchemaFromTypedSchema = (schema) => {
|
|
69
47
|
const item = schema;
|
|
70
|
-
if (
|
|
71
|
-
if (!
|
|
72
|
-
|
|
73
|
-
return item.getModel().getCleanSchema();
|
|
48
|
+
if (!schema[Symbol.metadata]) return item;
|
|
49
|
+
if (!schema[Symbol.metadata]._isTypedSchema) return item;
|
|
50
|
+
return schema[Symbol.metadata]._getModel().getSchema();
|
|
74
51
|
};
|
|
75
52
|
|
|
76
53
|
// src/getValidationErrors/getError/index.ts
|
|
@@ -104,7 +81,8 @@ function fieldType(opts) {
|
|
|
104
81
|
name,
|
|
105
82
|
validate: overwrittenValidate,
|
|
106
83
|
clean: overwrittenClean,
|
|
107
|
-
|
|
84
|
+
__isFieldType: true,
|
|
85
|
+
__tsFieldType: null
|
|
108
86
|
};
|
|
109
87
|
}
|
|
110
88
|
|
|
@@ -179,12 +157,12 @@ var string_default = fieldType({
|
|
|
179
157
|
name: "string",
|
|
180
158
|
validate(value, { currentSchema }) {
|
|
181
159
|
if (!isString(value)) return Errors_default.NOT_A_STRING;
|
|
182
|
-
if (isFinite(currentSchema.min)) {
|
|
160
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
183
161
|
if (value.length < currentSchema.min) {
|
|
184
162
|
return Errors_default.STRING_TOO_SHORT;
|
|
185
163
|
}
|
|
186
164
|
}
|
|
187
|
-
if (isFinite(currentSchema.max)) {
|
|
165
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
188
166
|
if (value.length > currentSchema.max) {
|
|
189
167
|
return Errors_default.STRING_TOO_LONG;
|
|
190
168
|
}
|
|
@@ -225,13 +203,13 @@ var date_default = fieldType({
|
|
|
225
203
|
if (options.autoConvert) {
|
|
226
204
|
if (isString2(value)) {
|
|
227
205
|
const result = new Date(value);
|
|
228
|
-
if (isNaN(result.getTime())) {
|
|
206
|
+
if (Number.isNaN(result.getTime())) {
|
|
229
207
|
return value;
|
|
230
208
|
}
|
|
231
209
|
value = result;
|
|
232
210
|
} else if (isNumber(value)) {
|
|
233
211
|
const result = new Date(value);
|
|
234
|
-
if (isNaN(result.getTime())) {
|
|
212
|
+
if (Number.isNaN(result.getTime())) {
|
|
235
213
|
return value;
|
|
236
214
|
}
|
|
237
215
|
value = result;
|
|
@@ -245,18 +223,17 @@ var date_default = fieldType({
|
|
|
245
223
|
import isInteger from "lodash/isInteger";
|
|
246
224
|
|
|
247
225
|
// src/fieldTypes/number.ts
|
|
248
|
-
import isFinite2 from "lodash/isFinite";
|
|
249
226
|
import toNumber from "lodash/toNumber";
|
|
250
227
|
var number_default = fieldType({
|
|
251
228
|
name: "number",
|
|
252
229
|
validate(value, { currentSchema }) {
|
|
253
|
-
if (!
|
|
254
|
-
if (
|
|
230
|
+
if (!Number.isFinite(value)) return Errors_default.NOT_A_NUMBER;
|
|
231
|
+
if (Number.isFinite(currentSchema.min)) {
|
|
255
232
|
if (value < currentSchema.min) {
|
|
256
233
|
return Errors_default.NUMBER_TOO_SMALL;
|
|
257
234
|
}
|
|
258
235
|
}
|
|
259
|
-
if (
|
|
236
|
+
if (Number.isFinite(currentSchema.max)) {
|
|
260
237
|
if (value > currentSchema.max) {
|
|
261
238
|
return Errors_default.NUMBER_TOO_BIG;
|
|
262
239
|
}
|
|
@@ -311,10 +288,11 @@ var boolean_default = fieldType({
|
|
|
311
288
|
clean(value, { options }) {
|
|
312
289
|
if (options.autoConvert) {
|
|
313
290
|
if (typeof value === "string") {
|
|
314
|
-
|
|
291
|
+
const stringValue = value;
|
|
292
|
+
if (stringValue === "true") {
|
|
315
293
|
value = true;
|
|
316
294
|
}
|
|
317
|
-
if (
|
|
295
|
+
if (stringValue === "false") {
|
|
318
296
|
value = false;
|
|
319
297
|
}
|
|
320
298
|
}
|
|
@@ -363,7 +341,7 @@ var blackbox_default = fieldType({
|
|
|
363
341
|
// src/fieldTypes/any.ts
|
|
364
342
|
var any_default = fieldType({
|
|
365
343
|
name: "any",
|
|
366
|
-
validate(
|
|
344
|
+
validate() {
|
|
367
345
|
}
|
|
368
346
|
});
|
|
369
347
|
|
|
@@ -384,19 +362,19 @@ var fieldTypes_default = {
|
|
|
384
362
|
|
|
385
363
|
// src/getValidationErrors/getError/getFieldValidator.ts
|
|
386
364
|
import has from "lodash/has";
|
|
387
|
-
function
|
|
365
|
+
function getFieldValidator(type) {
|
|
388
366
|
if (isPlainObject4(type)) {
|
|
389
|
-
if (type.
|
|
367
|
+
if (type.__isFieldType) return "custom";
|
|
390
368
|
return "plainObject";
|
|
391
369
|
}
|
|
392
370
|
if (isArray3(type)) return "array";
|
|
393
371
|
if (type === String) return "string";
|
|
394
|
-
if (type === Date) return "date";
|
|
372
|
+
if (typeof type === "function" && type.name === "Date") return "date";
|
|
395
373
|
if (type === Number) return "number";
|
|
396
374
|
if (type === Boolean) return "boolean";
|
|
397
375
|
if (type === "enum") return "string";
|
|
398
376
|
if (!isString5(type)) {
|
|
399
|
-
throw new Error(
|
|
377
|
+
throw new Error(`Field type is invalid. Pass a string or a custom field type. Got ${type}`);
|
|
400
378
|
}
|
|
401
379
|
const exists = has(fieldTypes_default, type);
|
|
402
380
|
if (!exists) {
|
|
@@ -414,7 +392,7 @@ async function getValidationErrors(params) {
|
|
|
414
392
|
return Errors_default.REQUIRED;
|
|
415
393
|
}
|
|
416
394
|
} else {
|
|
417
|
-
const validatorKey =
|
|
395
|
+
const validatorKey = getFieldValidator(currentSchema.type);
|
|
418
396
|
const validator = validatorKey === "custom" ? currentSchema.type : fieldTypes_default[validatorKey];
|
|
419
397
|
const error = await validator.validate(value, info, ...args);
|
|
420
398
|
if (error) {
|
|
@@ -444,8 +422,22 @@ import isArray4 from "lodash/isArray";
|
|
|
444
422
|
import clone from "lodash/clone";
|
|
445
423
|
import isNil3 from "lodash/isNil";
|
|
446
424
|
import difference2 from "lodash/difference";
|
|
425
|
+
|
|
426
|
+
// src/getValidationErrors/convertTypedSchema.ts
|
|
427
|
+
var convertOnParam = (info, paramName) => {
|
|
428
|
+
if (!info[paramName]) return;
|
|
429
|
+
const type = info[paramName].type;
|
|
430
|
+
if (!type) return;
|
|
431
|
+
info[paramName].type = getSchemaFromTypedSchema(type);
|
|
432
|
+
};
|
|
433
|
+
var convertTypedSchema = (info) => {
|
|
434
|
+
convertOnParam(info, "schema");
|
|
435
|
+
convertOnParam(info, "currentSchema");
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
// src/getValidationErrors/doValidation.ts
|
|
447
439
|
async function doValidation(params) {
|
|
448
|
-
|
|
440
|
+
convertTypedSchema(params);
|
|
449
441
|
const { schema, doc, currentDoc, value, currentSchema, keys = [], addError, options, args } = params;
|
|
450
442
|
const info = { schema, doc, currentDoc, value, currentSchema, keys, options, args, addError };
|
|
451
443
|
const error = await getValidationErrors(info);
|
|
@@ -457,7 +449,7 @@ async function doValidation(params) {
|
|
|
457
449
|
if (isPlainObject5(currentSchema.type)) {
|
|
458
450
|
const type = currentSchema.type;
|
|
459
451
|
if (type) {
|
|
460
|
-
if (type.
|
|
452
|
+
if (type.__isFieldType) {
|
|
461
453
|
return;
|
|
462
454
|
}
|
|
463
455
|
if (typeof type.__skipChildValidation === "function") {
|
|
@@ -521,10 +513,10 @@ var defaultOptions = {
|
|
|
521
513
|
omitRequired: false
|
|
522
514
|
};
|
|
523
515
|
async function getValidationErrors2(schema, doc, passedOptions = {}, ...args) {
|
|
524
|
-
schema =
|
|
516
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
525
517
|
const options = { ...defaultOptions, ...passedOptions };
|
|
526
518
|
const errors = [];
|
|
527
|
-
const addError =
|
|
519
|
+
const addError = (keys, code) => {
|
|
528
520
|
errors.push({
|
|
529
521
|
key: keys.join("."),
|
|
530
522
|
code
|
|
@@ -559,7 +551,7 @@ async function isValid(schema, doc, passedOptions = {}, ...args) {
|
|
|
559
551
|
|
|
560
552
|
// src/getValidationErrors/getError/getFieldType.ts
|
|
561
553
|
function getFieldType(type) {
|
|
562
|
-
const validatorKey =
|
|
554
|
+
const validatorKey = getFieldValidator(type);
|
|
563
555
|
const validator = validatorKey === "custom" ? type : fieldTypes_default[validatorKey];
|
|
564
556
|
return validator;
|
|
565
557
|
}
|
|
@@ -606,11 +598,6 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
606
598
|
value = defaultValue;
|
|
607
599
|
}
|
|
608
600
|
}
|
|
609
|
-
const { autoValue } = fieldSchema;
|
|
610
|
-
if (autoValue) {
|
|
611
|
-
needReClean = true;
|
|
612
|
-
value = await autoValue(value, info, ...args);
|
|
613
|
-
}
|
|
614
601
|
const { clean: clean3 } = fieldSchema;
|
|
615
602
|
if (clean3) {
|
|
616
603
|
needReClean = true;
|
|
@@ -624,11 +611,11 @@ async function cleanType(type, fieldSchema, value, info, ...args) {
|
|
|
624
611
|
|
|
625
612
|
// src/clean/recursiveClean.ts
|
|
626
613
|
import isNil5 from "lodash/isNil";
|
|
627
|
-
var cleanObjectFields = async
|
|
614
|
+
var cleanObjectFields = async ({
|
|
628
615
|
schema,
|
|
629
616
|
value,
|
|
630
617
|
...other
|
|
631
|
-
}) {
|
|
618
|
+
}) => {
|
|
632
619
|
const keys = Object.keys(schema.type).filter((key) => !key.startsWith("__"));
|
|
633
620
|
const newDoc = {};
|
|
634
621
|
for (const key of keys) {
|
|
@@ -649,13 +636,13 @@ var cleanObjectFields = async function({
|
|
|
649
636
|
}
|
|
650
637
|
return newDoc;
|
|
651
638
|
};
|
|
652
|
-
var cleanArrayItems = async
|
|
639
|
+
var cleanArrayItems = async ({
|
|
653
640
|
schema,
|
|
654
641
|
value,
|
|
655
642
|
...other
|
|
656
|
-
}) {
|
|
643
|
+
}) => {
|
|
657
644
|
const schemaType = schema.type[0];
|
|
658
|
-
const promises = value.map(async (item
|
|
645
|
+
const promises = value.map(async (item) => {
|
|
659
646
|
const newValue = await clean({
|
|
660
647
|
...other,
|
|
661
648
|
schema: {
|
|
@@ -676,9 +663,9 @@ function getArrayNode(schema, value) {
|
|
|
676
663
|
}
|
|
677
664
|
return null;
|
|
678
665
|
}
|
|
679
|
-
var clean = async
|
|
680
|
-
|
|
681
|
-
|
|
666
|
+
var clean = async (info) => {
|
|
667
|
+
convertTypedSchema(info);
|
|
668
|
+
const { schema, args = [], value } = info;
|
|
682
669
|
const currSchema = schema.type === void 0 ? { type: schema } : schema;
|
|
683
670
|
const objectSchema = getObjectNode(currSchema, value);
|
|
684
671
|
if (objectSchema) {
|
|
@@ -718,7 +705,7 @@ var defaultOptions2 = {
|
|
|
718
705
|
};
|
|
719
706
|
async function clean2(schema, doc, opts = {}, ...args) {
|
|
720
707
|
if (!doc) return doc;
|
|
721
|
-
schema =
|
|
708
|
+
schema = getSchemaFromTypedSchema(schema);
|
|
722
709
|
const options = { ...defaultOptions2, ...opts };
|
|
723
710
|
const params = {
|
|
724
711
|
schema: { type: schema },
|
|
@@ -743,7 +730,8 @@ var dotGet = function dotGet2(object, path) {
|
|
|
743
730
|
const levelObject = object.type;
|
|
744
731
|
if (first === "$" || /^[0-9]+$/.test(first)) {
|
|
745
732
|
return dotGet2({ type: levelObject[0] }, remainingPath);
|
|
746
|
-
}
|
|
733
|
+
}
|
|
734
|
+
if (isPlainObject7(levelObject[first])) {
|
|
747
735
|
return dotGet2(levelObject[first], remainingPath);
|
|
748
736
|
}
|
|
749
737
|
if (levelObject === "blackbox") {
|
|
@@ -789,9 +777,8 @@ async function validateKey_default(schema, key, value, passedOptions = {}, ...ar
|
|
|
789
777
|
if (!keySchema) {
|
|
790
778
|
if (options.filter) {
|
|
791
779
|
return Errors_default.NOT_IN_SCHEMA;
|
|
792
|
-
} else {
|
|
793
|
-
return null;
|
|
794
780
|
}
|
|
781
|
+
return null;
|
|
795
782
|
}
|
|
796
783
|
if (keySchema.isBlackboxChild) {
|
|
797
784
|
return null;
|
|
@@ -853,15 +840,91 @@ function createEnum(name, values) {
|
|
|
853
840
|
})
|
|
854
841
|
};
|
|
855
842
|
}
|
|
843
|
+
|
|
844
|
+
// src/models.ts
|
|
845
|
+
Symbol.metadata ?? (Symbol.metadata = Symbol("Symbol.metadata"));
|
|
846
|
+
function isSchemaLike(type) {
|
|
847
|
+
var _a;
|
|
848
|
+
if (!type) return false;
|
|
849
|
+
if (objectHasSubObjectWithKey(type, "type")) return true;
|
|
850
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) return true;
|
|
851
|
+
if (type.getModel) return true;
|
|
852
|
+
if (type.getSchema) return true;
|
|
853
|
+
if (type.getCleanSchema) return true;
|
|
854
|
+
if (type.__isModel) return true;
|
|
855
|
+
if (type.__modelName) return true;
|
|
856
|
+
return false;
|
|
857
|
+
}
|
|
858
|
+
function isSchemaOrFieldLike(type) {
|
|
859
|
+
if (Array.isArray(type)) {
|
|
860
|
+
if (type.length !== 1) return false;
|
|
861
|
+
return isSchemaOrFieldLike(type[0]);
|
|
862
|
+
}
|
|
863
|
+
if (isSchemaLike(type)) return true;
|
|
864
|
+
try {
|
|
865
|
+
if (getFieldValidator(type)) return true;
|
|
866
|
+
} catch {
|
|
867
|
+
return false;
|
|
868
|
+
}
|
|
869
|
+
return false;
|
|
870
|
+
}
|
|
871
|
+
function getSchemaModelName(type) {
|
|
872
|
+
if (!type) return null;
|
|
873
|
+
if (type.__modelName) return type.__modelName;
|
|
874
|
+
if (type.getModel) return type.getModel().name;
|
|
875
|
+
if (type.getSchema) return type.getSchema().__modelName;
|
|
876
|
+
return null;
|
|
877
|
+
}
|
|
878
|
+
function getSchemaFromAnyOrionForm(type) {
|
|
879
|
+
var _a, _b;
|
|
880
|
+
if ((_a = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _a._getModel) {
|
|
881
|
+
return (_b = type == null ? void 0 : type[Symbol.metadata]) == null ? void 0 : _b._getModel().getSchema();
|
|
882
|
+
}
|
|
883
|
+
if (type == null ? void 0 : type.getModel) return type.getModel().getSchema();
|
|
884
|
+
if (type.getSchema) {
|
|
885
|
+
return type.getSchema();
|
|
886
|
+
}
|
|
887
|
+
if (type.getSchema) {
|
|
888
|
+
return type.getSchema();
|
|
889
|
+
}
|
|
890
|
+
if (objectHasSubObjectWithKey(type, "type")) return type;
|
|
891
|
+
return null;
|
|
892
|
+
}
|
|
893
|
+
function objectHasSubObjectWithKey(object, key) {
|
|
894
|
+
if (!object || typeof object !== "object") return false;
|
|
895
|
+
for (const key1 in object) {
|
|
896
|
+
const value = object[key1];
|
|
897
|
+
if (value && typeof value === "object" && key in value) {
|
|
898
|
+
return true;
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
return false;
|
|
902
|
+
}
|
|
903
|
+
function getSchemaWithMetadataFromAnyOrionForm(type) {
|
|
904
|
+
return getSchemaFromAnyOrionForm(type);
|
|
905
|
+
}
|
|
906
|
+
|
|
907
|
+
// src/schemaWithName/index.ts
|
|
908
|
+
function schemaWithName(name, schema) {
|
|
909
|
+
schema.__modelName = name;
|
|
910
|
+
return schema;
|
|
911
|
+
}
|
|
856
912
|
export {
|
|
857
913
|
ValidationError,
|
|
858
914
|
clean2 as clean,
|
|
859
915
|
cleanKey_default as cleanKey,
|
|
860
916
|
createEnum,
|
|
861
917
|
dotGetSchema_default2 as dotGetSchema,
|
|
918
|
+
fieldTypes_default as fieldTypes,
|
|
862
919
|
getFieldType,
|
|
920
|
+
getSchemaFromAnyOrionForm,
|
|
921
|
+
getSchemaModelName,
|
|
922
|
+
getSchemaWithMetadataFromAnyOrionForm,
|
|
863
923
|
getValidationErrors2 as getValidationErrors,
|
|
924
|
+
isSchemaLike,
|
|
925
|
+
isSchemaOrFieldLike,
|
|
864
926
|
isValid,
|
|
927
|
+
schemaWithName,
|
|
865
928
|
validate,
|
|
866
929
|
validateKey_default as validateKey
|
|
867
930
|
};
|