@forklaunch/validator 0.2.8 → 0.2.9
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.
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
1
2
|
import { SchemaValidator } from '../index';
|
|
2
3
|
import { LiteralSchema } from '../types/schema.types';
|
|
3
4
|
declare module '../types/schema.types' {
|
|
@@ -8,7 +9,11 @@ declare module '../types/schema.types' {
|
|
|
8
9
|
Mock: T;
|
|
9
10
|
}
|
|
10
11
|
}
|
|
11
|
-
|
|
12
|
+
type RecursiveUnion<T extends readonly string[]> = T extends readonly [
|
|
13
|
+
infer F extends string,
|
|
14
|
+
...infer R extends readonly string[]
|
|
15
|
+
] ? R extends [] ? F : `${F} | ${RecursiveUnion<R>}` : '';
|
|
16
|
+
export declare class MockSchemaValidator implements SchemaValidator<(<T extends string>(schema: T) => T), <T extends string>(schema: T) => `optional ${T}`, <T extends string>(schema: T) => `array ${T}`, <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>, <T extends LiteralSchema>(schema: T) => `literal ${T}`, <T extends string>(schema: T, value: string) => boolean, <T extends string>(schema: T) => SchemaObject> {
|
|
12
17
|
_Type: 'Mock';
|
|
13
18
|
_SchemaCatchall: string;
|
|
14
19
|
_ValidSchemaObject: string;
|
|
@@ -23,12 +28,12 @@ export declare class MockSchemaValidator implements SchemaValidator {
|
|
|
23
28
|
unknown: string;
|
|
24
29
|
never: string;
|
|
25
30
|
schemify<T>(schema: T): T;
|
|
26
|
-
optional<T>(schema: T):
|
|
27
|
-
array<T>(schema: T):
|
|
28
|
-
union<T>(schemas: T
|
|
31
|
+
optional<T extends string>(schema: T): `optional ${T}`;
|
|
32
|
+
array<T extends string>(schema: T): `array ${T}`;
|
|
33
|
+
union<T extends readonly string[]>(schemas: T): RecursiveUnion<T>;
|
|
29
34
|
literal<T extends LiteralSchema>(schema: T): `literal ${T}`;
|
|
30
|
-
validate<T>(schema: T): boolean;
|
|
31
|
-
openapi<T>(schema: T):
|
|
35
|
+
validate<T extends string>(schema: T, value: string): boolean;
|
|
36
|
+
openapi<T extends string>(schema: T): SchemaObject;
|
|
32
37
|
}
|
|
33
38
|
export declare const mockSchemaValidator: MockSchemaValidator;
|
|
34
39
|
export declare const string: string;
|
|
@@ -42,9 +47,10 @@ export declare const any: string;
|
|
|
42
47
|
export declare const unknown: string;
|
|
43
48
|
export declare const never: string;
|
|
44
49
|
export declare const schemify: <T>(schema: T) => T;
|
|
45
|
-
export declare const optional: <T>(schema: T) =>
|
|
46
|
-
export declare const array: <T>(schema: T) =>
|
|
47
|
-
export declare const union: <T>(schemas: T
|
|
50
|
+
export declare const optional: <T extends string>(schema: T) => `optional ${T}`;
|
|
51
|
+
export declare const array: <T extends string>(schema: T) => `array ${T}`;
|
|
52
|
+
export declare const union: <T extends readonly string[]>(schemas: T) => RecursiveUnion<T>;
|
|
48
53
|
export declare const literal: <T extends LiteralSchema>(schema: T) => `literal ${T}`;
|
|
49
|
-
export declare const validate: <T>(schema: T) => boolean;
|
|
50
|
-
export declare const openapi: <T>(schema: T) =>
|
|
54
|
+
export declare const validate: <T extends string>(schema: T, value: string) => boolean;
|
|
55
|
+
export declare const openapi: <T extends string>(schema: T) => SchemaObject;
|
|
56
|
+
export {};
|
|
@@ -19,10 +19,10 @@ class MockSchemaValidator {
|
|
|
19
19
|
return schema;
|
|
20
20
|
}
|
|
21
21
|
optional(schema) {
|
|
22
|
-
return 'optional ' + schema;
|
|
22
|
+
return ('optional ' + schema);
|
|
23
23
|
}
|
|
24
24
|
array(schema) {
|
|
25
|
-
return 'array ' + schema;
|
|
25
|
+
return ('array ' + schema);
|
|
26
26
|
}
|
|
27
27
|
union(schemas) {
|
|
28
28
|
return schemas.join(' | ');
|
|
@@ -30,7 +30,7 @@ class MockSchemaValidator {
|
|
|
30
30
|
literal(schema) {
|
|
31
31
|
return `literal ${schema}`;
|
|
32
32
|
}
|
|
33
|
-
validate(schema) {
|
|
33
|
+
validate(schema, value) {
|
|
34
34
|
return true;
|
|
35
35
|
}
|
|
36
36
|
openapi(schema) {
|
|
@@ -52,6 +52,7 @@ exports.never = exports.mockSchemaValidator.never;
|
|
|
52
52
|
exports.schemify = exports.mockSchemaValidator.schemify.bind(exports.mockSchemaValidator);
|
|
53
53
|
exports.optional = exports.mockSchemaValidator.optional.bind(exports.mockSchemaValidator);
|
|
54
54
|
exports.array = exports.mockSchemaValidator.array.bind(exports.mockSchemaValidator);
|
|
55
|
+
// note, use 'as const' when calling on the input array, for proper type coercion
|
|
55
56
|
exports.union = exports.mockSchemaValidator.union.bind(exports.mockSchemaValidator);
|
|
56
57
|
exports.literal = exports.mockSchemaValidator.literal.bind(exports.mockSchemaValidator);
|
|
57
58
|
exports.validate = exports.mockSchemaValidator.validate.bind(exports.mockSchemaValidator);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mockSchemaValidator.js","sourceRoot":"","sources":["../../tests/mockSchemaValidator.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"mockSchemaValidator.js","sourceRoot":"","sources":["../../tests/mockSchemaValidator.ts"],"names":[],"mappings":";;;AAuBA,MAAa,mBAAmB;IAY9B,KAAK,CAAU;IACf,eAAe,CAAU;IACzB,kBAAkB,CAAU;IAE5B,MAAM,GAAG,QAAQ,CAAC;IAClB,MAAM,GAAG,QAAQ,CAAC;IAClB,MAAM,GAAG,QAAQ,CAAC;IAClB,OAAO,GAAG,SAAS,CAAC;IACpB,IAAI,GAAG,MAAM,CAAC;IACd,MAAM,GAAG,QAAQ,CAAC;IAClB,KAAK,GAAG,OAAO,CAAC;IAChB,GAAG,GAAG,KAAK,CAAC;IACZ,OAAO,GAAG,SAAS,CAAC;IACpB,KAAK,GAAG,OAAO,CAAC;IAEhB,QAAQ,CAAI,MAAS;QACnB,OAAO,MAAM,CAAC;IAChB,CAAC;IACD,QAAQ,CAAmB,MAAS;QAClC,OAAO,C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AAC,IAAI,CAAC,2BAAmB,CAAC,CAAC;AACzE,iFAAiF;AACpE,QAAA,KAAK,GAAG,2BAAmB,CAAC,KAAK,CAAC,IAAI,CAAC,2BAAmB,CAAC,CAAC;AAC5D,QAAA,OAAO,GAAG,2BAAmB,CAAC,OAAO,CAAC,IAAI,CAAC,2BAAmB,CAAC,CAAC;AAChE,QAAA,QAAQ,GAAG,2BAAmB,CAAC,QAAQ,CAAC,IAAI,CAAC,2BAAmB,CAAC,CAAC;AAClE,QAAA,OAAO,GAAG,2BAAmB,CAAC,OAAO,CAAC,IAAI,CAAC,2BAAmB,CAAC,CAAC"}
|
|
@@ -111,7 +111,9 @@ export interface SchemaValidator<SchematicFunction = <T>(schema: T) => unknown,
|
|
|
111
111
|
*/
|
|
112
112
|
openapi: OpenAPIFunction;
|
|
113
113
|
}
|
|
114
|
-
export type AnySchemaValidator = SchemaValidator<unknown, unknown, unknown, unknown, unknown, unknown, unknown
|
|
114
|
+
export type AnySchemaValidator = SchemaValidator<unknown, unknown, unknown, unknown, unknown, unknown, unknown> & {
|
|
115
|
+
_Type: keyof SchemaResolve<unknown>;
|
|
116
|
+
};
|
|
115
117
|
export interface SchemaResolve<T> {
|
|
116
118
|
Zod: ZodResolve<T>;
|
|
117
119
|
TypeBox: TResolve<T>;
|
|
@@ -120,12 +122,8 @@ export interface SchemaTranslate<T> {
|
|
|
120
122
|
Zod: ZodSchemaTranslate<T>;
|
|
121
123
|
TypeBox: TSchemaTranslate<T>;
|
|
122
124
|
}
|
|
123
|
-
type SchemaPrettify<T, SV extends
|
|
124
|
-
|
|
125
|
-
} & AnySchemaValidator> = Prettify<SchemaTranslate<T>[SV['_Type']]>;
|
|
126
|
-
export type Schema<T extends SV['_ValidSchemaObject'] | IdiomaticSchema<SV>, SV extends {
|
|
127
|
-
_Type: keyof SchemaResolve<T>;
|
|
128
|
-
} & AnySchemaValidator> = SchemaPrettify<SchemaResolve<T>[SV['_Type']], SV>;
|
|
125
|
+
type SchemaPrettify<T, SV extends AnySchemaValidator> = Prettify<SchemaTranslate<T>[SV['_Type']]>;
|
|
126
|
+
export type Schema<T extends SV['_ValidSchemaObject'] | IdiomaticSchema<SV>, SV extends AnySchemaValidator> = SchemaPrettify<SchemaResolve<T>[SV['_Type']], SV>;
|
|
129
127
|
/**
|
|
130
128
|
* Represents a schema for an unboxed object where each key can have an idiomatic schema.
|
|
131
129
|
*
|