typescript-type 0.0.14 → 0.0.15
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/package.json +1 -1
- package/patterns/driver.ts +10 -3
- package/patterns/validator.ts +29 -12
package/package.json
CHANGED
package/patterns/driver.ts
CHANGED
|
@@ -64,11 +64,18 @@ export abstract class Driver<Config = any> {
|
|
|
64
64
|
): T => {
|
|
65
65
|
try {
|
|
66
66
|
// Check if it's a SchemaDefinition (has .parse method)
|
|
67
|
-
if ('parse' in schema && typeof schema.parse === 'function') {
|
|
67
|
+
if (schema && typeof schema === 'object' && 'parse' in schema && typeof (schema as any).parse === 'function') {
|
|
68
68
|
return (schema as SchemaDefinition<T>).parse(rawData);
|
|
69
69
|
}
|
|
70
|
-
//
|
|
71
|
-
|
|
70
|
+
// Check if schema is callable (arktype Type)
|
|
71
|
+
if (typeof schema === 'function') {
|
|
72
|
+
return parseSync(schema as Type<T>, rawData);
|
|
73
|
+
}
|
|
74
|
+
// Try using .schema property if available (SchemaDefinition)
|
|
75
|
+
if (schema && typeof schema === 'object' && 'schema' in schema) {
|
|
76
|
+
return parseSync((schema as any).schema as Type<T>, rawData);
|
|
77
|
+
}
|
|
78
|
+
throw new Error(`Invalid schema type: ${typeof schema}`);
|
|
72
79
|
} catch (error: any) {
|
|
73
80
|
throw new BadConfigException([error.message || String(error)]);
|
|
74
81
|
}
|
package/patterns/validator.ts
CHANGED
|
@@ -35,31 +35,44 @@ export type SafeParseResult<T> = ValidationResult<T> | ValidationError;
|
|
|
35
35
|
// Core Validator Factory
|
|
36
36
|
// ============================================================================
|
|
37
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Check if a value is an arktype Type object (not a lazy factory function)
|
|
40
|
+
* Arktype Types are callable but also have special properties like 'expression'
|
|
41
|
+
*/
|
|
42
|
+
function isArktypeType(value: unknown): boolean {
|
|
43
|
+
return (
|
|
44
|
+
typeof value === 'function' &&
|
|
45
|
+
value !== null &&
|
|
46
|
+
typeof (value as any).expression === 'string'
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
|
|
38
50
|
export function createValidator<T>(schema: any) {
|
|
39
51
|
const syncChecks: SyncCheck<T>[] = [];
|
|
40
52
|
const asyncChecks: AsyncCheck<T>[] = [];
|
|
41
53
|
let lazySchemaCache: any = null;
|
|
42
54
|
|
|
43
55
|
function valueData(data: T) {
|
|
44
|
-
|
|
45
|
-
|
|
56
|
+
// If schema is a lazy factory function (not an arktype Type), call it to get the actual schema
|
|
57
|
+
const currentSchema = (typeof schema === 'function' && !isArktypeType(schema))
|
|
58
|
+
? schema()
|
|
59
|
+
: schema;
|
|
60
|
+
|
|
61
|
+
// Arktype Types are callable - call them directly with the data
|
|
46
62
|
let result: SchemaResult<T>;
|
|
47
|
-
if (
|
|
63
|
+
if (isArktypeType(currentSchema)) {
|
|
64
|
+
// Arktype Type - call it directly
|
|
65
|
+
result = currentSchema(data);
|
|
66
|
+
} else if (typeof currentSchema === 'function') {
|
|
67
|
+
// Some other callable schema
|
|
48
68
|
result = currentSchema(data);
|
|
49
69
|
} else if (currentSchema && typeof currentSchema.assert === 'function') {
|
|
50
|
-
//
|
|
70
|
+
// Schema with .assert method
|
|
51
71
|
try {
|
|
52
72
|
result = { data: currentSchema.assert(data) };
|
|
53
73
|
} catch (e: any) {
|
|
54
74
|
result = { problems: { summary: e.message || String(e) } };
|
|
55
75
|
}
|
|
56
|
-
} else if (currentSchema && typeof currentSchema === 'object') {
|
|
57
|
-
// Try calling it as a function (arktype types are callable)
|
|
58
|
-
try {
|
|
59
|
-
result = currentSchema(data);
|
|
60
|
-
} catch (e: any) {
|
|
61
|
-
throw new Error(`Schema is not callable: ${e.message || String(e)}`);
|
|
62
|
-
}
|
|
63
76
|
} else {
|
|
64
77
|
throw new Error('Invalid schema provided to validator');
|
|
65
78
|
}
|
|
@@ -119,7 +132,11 @@ function _runValidation(
|
|
|
119
132
|
data: unknown,
|
|
120
133
|
options: ParseOptionsSync = {}
|
|
121
134
|
): { data: any; validator: ReturnType<typeof createValidator> } {
|
|
122
|
-
|
|
135
|
+
// If it's a function but NOT an arktype Type, assume it's a lazy factory
|
|
136
|
+
// Arktype Types are callable but have an 'expression' property
|
|
137
|
+
const schema = (typeof types === 'function' && !isArktypeType(types))
|
|
138
|
+
? types()
|
|
139
|
+
: types;
|
|
123
140
|
const validator = options.validator ?? createValidator(schema);
|
|
124
141
|
if (options.checks) for (const fn of options.checks) validator.check(fn);
|
|
125
142
|
const result = validator.validate(data);
|