pqb 0.7.13 → 0.8.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/CHANGELOG.md +12 -0
- package/dist/index.d.ts +618 -563
- package/dist/index.esm.js +1011 -402
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1014 -401
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/array.test.ts +67 -0
- package/src/columnSchema/array.ts +39 -13
- package/src/columnSchema/boolean.test.ts +17 -0
- package/src/columnSchema/boolean.ts +5 -1
- package/src/columnSchema/columnType.test.ts +230 -107
- package/src/columnSchema/columnType.ts +198 -28
- package/src/columnSchema/columnTypes.ts +28 -15
- package/src/columnSchema/columnsSchema.ts +6 -4
- package/src/columnSchema/commonMethods.ts +11 -4
- package/src/columnSchema/dateTime.test.ts +298 -0
- package/src/columnSchema/dateTime.ts +59 -2
- package/src/columnSchema/enum.test.ts +33 -0
- package/src/columnSchema/enum.ts +11 -1
- package/src/columnSchema/json/array.test.ts +21 -0
- package/src/columnSchema/json/array.ts +27 -13
- package/src/columnSchema/json/discriminatedUnion.test.ts +32 -0
- package/src/columnSchema/json/discriminatedUnion.ts +17 -2
- package/src/columnSchema/json/enum.test.ts +9 -0
- package/src/columnSchema/json/enum.ts +9 -1
- package/src/columnSchema/json/index.ts +19 -19
- package/src/columnSchema/json/instanceOf.test.ts +8 -0
- package/src/columnSchema/json/instanceOf.ts +4 -1
- package/src/columnSchema/json/intersection.test.ts +19 -0
- package/src/columnSchema/json/intersection.ts +9 -1
- package/src/columnSchema/json/lazy.test.ts +22 -0
- package/src/columnSchema/json/lazy.ts +22 -1
- package/src/columnSchema/json/literal.test.ts +7 -0
- package/src/columnSchema/json/literal.ts +12 -1
- package/src/columnSchema/json/map.test.ts +10 -0
- package/src/columnSchema/json/map.ts +21 -1
- package/src/columnSchema/json/nativeEnum.test.ts +10 -0
- package/src/columnSchema/json/nativeEnum.ts +4 -1
- package/src/columnSchema/json/nullable.test.ts +18 -0
- package/src/columnSchema/json/nullish.test.ts +18 -0
- package/src/columnSchema/json/object.test.ts +77 -0
- package/src/columnSchema/json/object.ts +31 -3
- package/src/columnSchema/json/optional.test.ts +18 -0
- package/src/columnSchema/json/record.test.ts +14 -0
- package/src/columnSchema/json/record.ts +12 -1
- package/src/columnSchema/json/scalarTypes.test.ts +133 -0
- package/src/columnSchema/json/scalarTypes.ts +90 -1
- package/src/columnSchema/json/set.test.ts +29 -0
- package/src/columnSchema/json/set.ts +26 -7
- package/src/columnSchema/json/tuple.test.ts +17 -0
- package/src/columnSchema/json/tuple.ts +16 -1
- package/src/columnSchema/json/typeBase.test.ts +123 -0
- package/src/columnSchema/json/typeBase.ts +52 -13
- package/src/columnSchema/json/union.test.ts +10 -0
- package/src/columnSchema/json/union.ts +18 -1
- package/src/columnSchema/json.test.ts +17 -0
- package/src/columnSchema/json.ts +10 -2
- package/src/columnSchema/number.test.ts +176 -0
- package/src/columnSchema/number.ts +48 -1
- package/src/columnSchema/string.test.ts +412 -0
- package/src/columnSchema/string.ts +126 -15
- package/src/columnSchema/timestamps.test.ts +6 -6
- package/src/columnSchema/virtual.ts +4 -0
- package/src/db.ts +1 -1
- package/src/query.ts +1 -1
- package/src/queryMethods/create.ts +6 -6
- package/src/queryMethods/for.ts +3 -3
- package/src/queryMethods/having.ts +1 -1
- package/src/queryMethods/join.ts +4 -4
- package/src/queryMethods/json.ts +1 -1
- package/src/queryMethods/queryMethods.ts +2 -2
- package/src/queryMethods/select.ts +3 -3
- package/src/queryMethods/update.ts +17 -17
- package/src/queryMethods/where.test.ts +1 -1
- package/src/queryMethods/where.ts +4 -4
- package/src/relations.ts +1 -1
- package/src/sql/aggregate.ts +2 -2
- package/src/sql/copy.ts +3 -3
- package/src/sql/delete.ts +5 -5
- package/src/sql/fromAndAs.ts +4 -4
- package/src/sql/having.ts +7 -7
- package/src/sql/insert.ts +5 -5
- package/src/sql/join.ts +16 -16
- package/src/sql/select.ts +6 -6
- package/src/sql/toSql.ts +24 -24
- package/src/sql/update.ts +4 -4
- package/src/sql/where.ts +18 -18
- package/src/utils.test.ts +9 -0
- package/src/utils.ts +3 -0
- package/src/columnSchema/columnTypes.test.ts +0 -527
|
@@ -1,19 +1,19 @@
|
|
|
1
|
-
export * from './array'
|
|
2
|
-
export * from './discriminatedUnion'
|
|
3
|
-
export * from './enum'
|
|
4
|
-
export * from './instanceOf'
|
|
5
|
-
export * from './intersection'
|
|
6
|
-
export * from './lazy'
|
|
7
|
-
export * from './literal'
|
|
8
|
-
export * from './map'
|
|
9
|
-
export * from './nativeEnum'
|
|
10
|
-
export * from './nullable'
|
|
11
|
-
export * from './nullish'
|
|
12
|
-
export * from './object'
|
|
13
|
-
export * from './optional'
|
|
14
|
-
export * from './record'
|
|
15
|
-
export * from './scalarTypes'
|
|
16
|
-
export * from './set'
|
|
17
|
-
export * from './tuple'
|
|
18
|
-
export * from './typeBase'
|
|
19
|
-
export * from './union'
|
|
1
|
+
export * from './array';
|
|
2
|
+
export * from './discriminatedUnion';
|
|
3
|
+
export * from './enum';
|
|
4
|
+
export * from './instanceOf';
|
|
5
|
+
export * from './intersection';
|
|
6
|
+
export * from './lazy';
|
|
7
|
+
export * from './literal';
|
|
8
|
+
export * from './map';
|
|
9
|
+
export * from './nativeEnum';
|
|
10
|
+
export * from './nullable';
|
|
11
|
+
export * from './nullish';
|
|
12
|
+
export * from './object';
|
|
13
|
+
export * from './optional';
|
|
14
|
+
export * from './record';
|
|
15
|
+
export * from './scalarTypes';
|
|
16
|
+
export * from './set';
|
|
17
|
+
export * from './tuple';
|
|
18
|
+
export * from './typeBase';
|
|
19
|
+
export * from './union';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { constructType, JSONType } from './typeBase';
|
|
1
|
+
import { constructType, JSONType, toCode } from './typeBase';
|
|
2
2
|
|
|
3
3
|
export interface JSONInstanceOf<T extends Class>
|
|
4
4
|
extends JSONType<T, 'instanceOf'> {
|
|
@@ -12,5 +12,8 @@ export const instanceOf = <T extends Class>(cls: T) => {
|
|
|
12
12
|
return constructType<JSONInstanceOf<T>>({
|
|
13
13
|
dataType: 'instanceOf',
|
|
14
14
|
class: cls,
|
|
15
|
+
toCode(this: JSONInstanceOf<T>, t: string) {
|
|
16
|
+
return toCode(this, t, `${t}.instanceOf(${this.class.name})`);
|
|
17
|
+
},
|
|
15
18
|
});
|
|
16
19
|
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { intersection } from './intersection';
|
|
2
|
+
import { object } from './object';
|
|
3
|
+
import { scalarTypes } from './scalarTypes';
|
|
4
|
+
|
|
5
|
+
describe('intersection', () => {
|
|
6
|
+
it('should have toCode', () => {
|
|
7
|
+
expect(
|
|
8
|
+
intersection(
|
|
9
|
+
object({ name: scalarTypes.string() }),
|
|
10
|
+
object({ age: scalarTypes.number() }),
|
|
11
|
+
).toCode('t'),
|
|
12
|
+
).toEqual([
|
|
13
|
+
['t.object({', ['name: t.string(),'], '})'],
|
|
14
|
+
'.and(',
|
|
15
|
+
['t.object({', ['age: t.number(),'], '})'],
|
|
16
|
+
')',
|
|
17
|
+
]);
|
|
18
|
+
});
|
|
19
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { constructType, JSONType, JSONTypeAny } from './typeBase';
|
|
1
|
+
import { constructType, JSONType, JSONTypeAny, toCode } from './typeBase';
|
|
2
2
|
|
|
3
3
|
export type JSONIntersection<
|
|
4
4
|
Left extends JSONTypeAny,
|
|
@@ -19,5 +19,13 @@ export const intersection = <
|
|
|
19
19
|
dataType: 'intersection',
|
|
20
20
|
left,
|
|
21
21
|
right,
|
|
22
|
+
toCode(this: JSONIntersection<Left, Right>, t: string) {
|
|
23
|
+
return toCode(this, t, [
|
|
24
|
+
this.left.toCode(t),
|
|
25
|
+
'.and(',
|
|
26
|
+
this.right.toCode(t),
|
|
27
|
+
')',
|
|
28
|
+
]);
|
|
29
|
+
},
|
|
22
30
|
});
|
|
23
31
|
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { lazy } from './lazy';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('lazy', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(lazy(() => scalarTypes.string()).toCode('t')).toEqual([
|
|
7
|
+
't.lazy(() => ',
|
|
8
|
+
't.string()',
|
|
9
|
+
')',
|
|
10
|
+
]);
|
|
11
|
+
expect(
|
|
12
|
+
lazy(() => scalarTypes.string())
|
|
13
|
+
.deepPartial()
|
|
14
|
+
.toCode('t'),
|
|
15
|
+
).toEqual([
|
|
16
|
+
't.lazy(() => ',
|
|
17
|
+
't.string().optional()',
|
|
18
|
+
')',
|
|
19
|
+
'.deepPartial()',
|
|
20
|
+
]);
|
|
21
|
+
});
|
|
22
|
+
});
|
|
@@ -1,7 +1,17 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
constructType,
|
|
3
|
+
JSONType,
|
|
4
|
+
JSONTypeAny,
|
|
5
|
+
JSONTypeData,
|
|
6
|
+
toCode,
|
|
7
|
+
} from './typeBase';
|
|
8
|
+
import { toArray } from '../../utils';
|
|
2
9
|
|
|
3
10
|
export interface JSONLazy<T extends JSONTypeAny>
|
|
4
11
|
extends JSONType<T['type'], 'lazy'> {
|
|
12
|
+
data: JSONTypeData & {
|
|
13
|
+
isDeepPartial?: boolean;
|
|
14
|
+
};
|
|
5
15
|
typeCache?: T;
|
|
6
16
|
getter(): T;
|
|
7
17
|
deepPartial(): JSONLazy<ReturnType<T['deepPartial']>>;
|
|
@@ -13,9 +23,20 @@ export const lazy = <T extends JSONTypeAny>(fn: () => T): JSONLazy<T> => {
|
|
|
13
23
|
getter() {
|
|
14
24
|
return this.typeCache || (this.typeCache = fn());
|
|
15
25
|
},
|
|
26
|
+
toCode(this: JSONLazy<T>, t: string) {
|
|
27
|
+
return toCode(this, t, [
|
|
28
|
+
`${t}.lazy(() => `,
|
|
29
|
+
...toArray(this.getter().toCode(t)),
|
|
30
|
+
')',
|
|
31
|
+
]);
|
|
32
|
+
},
|
|
16
33
|
deepPartial(this: JSONLazy<T>) {
|
|
17
34
|
return {
|
|
18
35
|
...this,
|
|
36
|
+
data: {
|
|
37
|
+
...this.data,
|
|
38
|
+
isDeepPartial: true,
|
|
39
|
+
},
|
|
19
40
|
typeCache: undefined,
|
|
20
41
|
getter: () => this.getter().deepPartial(),
|
|
21
42
|
};
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { constructType, JSONType, Primitive } from './typeBase';
|
|
1
|
+
import { constructType, JSONType, Primitive, toCode } from './typeBase';
|
|
2
|
+
import { singleQuote } from '../../utils';
|
|
2
3
|
|
|
3
4
|
export interface JSONLiteral<T extends Primitive>
|
|
4
5
|
extends JSONType<T, 'literal'> {
|
|
@@ -9,4 +10,14 @@ export const literal = <T extends Primitive>(value: T) =>
|
|
|
9
10
|
constructType<JSONLiteral<T>>({
|
|
10
11
|
dataType: 'literal',
|
|
11
12
|
value,
|
|
13
|
+
toCode(this: JSONLiteral<T>, t: string) {
|
|
14
|
+
const { value } = this;
|
|
15
|
+
return toCode(
|
|
16
|
+
this,
|
|
17
|
+
t,
|
|
18
|
+
`${t}.literal(${
|
|
19
|
+
typeof value === 'string' ? singleQuote(value) : value
|
|
20
|
+
})`,
|
|
21
|
+
);
|
|
22
|
+
},
|
|
12
23
|
});
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { map } from './map';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('map', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(map(scalarTypes.string(), scalarTypes.number()).toCode('t')).toBe(
|
|
7
|
+
't.map(t.string(), t.number())',
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
constructType,
|
|
3
|
+
JSONType,
|
|
4
|
+
JSONTypeAny,
|
|
5
|
+
JSONTypeData,
|
|
6
|
+
toCode,
|
|
7
|
+
} from './typeBase';
|
|
2
8
|
|
|
3
9
|
export interface JSONMap<Key extends JSONTypeAny, Value extends JSONTypeAny>
|
|
4
10
|
extends JSONType<Map<Key['type'], Value['type']>, 'map'> {
|
|
11
|
+
data: JSONTypeData & {
|
|
12
|
+
isDeepPartial?: boolean;
|
|
13
|
+
};
|
|
5
14
|
keyType: Key;
|
|
6
15
|
valueType: Value;
|
|
7
16
|
deepPartial(): JSONMap<
|
|
@@ -18,9 +27,20 @@ export const map = <Key extends JSONTypeAny, Value extends JSONTypeAny>(
|
|
|
18
27
|
dataType: 'map',
|
|
19
28
|
keyType: keyType,
|
|
20
29
|
valueType: valueType,
|
|
30
|
+
toCode(this: JSONMap<Key, Value>, t: string) {
|
|
31
|
+
return toCode(
|
|
32
|
+
this,
|
|
33
|
+
t,
|
|
34
|
+
`${t}.map(${this.keyType.toCode(t)}, ${this.valueType.toCode(t)})`,
|
|
35
|
+
);
|
|
36
|
+
},
|
|
21
37
|
deepPartial(this: JSONMap<Key, Value>) {
|
|
22
38
|
return {
|
|
23
39
|
...this,
|
|
40
|
+
data: {
|
|
41
|
+
...this.data,
|
|
42
|
+
isDeepPartial: true,
|
|
43
|
+
},
|
|
24
44
|
keyType: this.keyType.deepPartial(),
|
|
25
45
|
valueType: this.valueType.deepPartial(),
|
|
26
46
|
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { constructType, JSONType } from './typeBase';
|
|
1
|
+
import { constructType, JSONType, toCode } from './typeBase';
|
|
2
2
|
|
|
3
3
|
export interface JSONNativeEnum<T extends EnumLike>
|
|
4
4
|
extends JSONType<T[keyof T], 'nativeEnum'> {
|
|
@@ -26,5 +26,8 @@ export const nativeEnum = <T extends EnumLike>(givenEnum: T) => {
|
|
|
26
26
|
dataType: 'nativeEnum',
|
|
27
27
|
enum: givenEnum,
|
|
28
28
|
options,
|
|
29
|
+
toCode(this: JSONNativeEnum<T>, t: string) {
|
|
30
|
+
return toCode(this, t, `${t}.nativeEnum(enum)`);
|
|
31
|
+
},
|
|
29
32
|
});
|
|
30
33
|
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { notNullable, nullable } from './nullable';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('nullable', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(nullable(scalarTypes.string()).toCode('t')).toBe(
|
|
7
|
+
't.string().nullable()',
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('notNullable', () => {
|
|
13
|
+
it('should have toCode', () => {
|
|
14
|
+
expect(notNullable(scalarTypes.string().nullable()).toCode('t')).toBe(
|
|
15
|
+
't.string()',
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { notNullish, nullish } from './nullish';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('nullish', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(nullish(scalarTypes.string()).toCode('t')).toBe(
|
|
7
|
+
't.string().nullish()',
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('notNullish', () => {
|
|
13
|
+
it('should have toCode', () => {
|
|
14
|
+
expect(notNullish(scalarTypes.string().nullish()).toCode('t')).toBe(
|
|
15
|
+
't.string()',
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { object } from './object';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('object', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
const shape = { key: scalarTypes.string() };
|
|
7
|
+
const other = { other: scalarTypes.number() };
|
|
8
|
+
|
|
9
|
+
expect(object(shape).toCode('t')).toEqual([
|
|
10
|
+
't.object({',
|
|
11
|
+
['key: t.string(),'],
|
|
12
|
+
'})',
|
|
13
|
+
]);
|
|
14
|
+
|
|
15
|
+
expect(object(shape).extend(other).toCode('t')).toEqual([
|
|
16
|
+
't.object({',
|
|
17
|
+
['key: t.string(),', 'other: t.number(),'],
|
|
18
|
+
'})',
|
|
19
|
+
]);
|
|
20
|
+
|
|
21
|
+
expect(object(shape).merge(object(other)).toCode('t')).toEqual([
|
|
22
|
+
't.object({',
|
|
23
|
+
['key: t.string(),', 'other: t.number(),'],
|
|
24
|
+
'})',
|
|
25
|
+
]);
|
|
26
|
+
|
|
27
|
+
expect(
|
|
28
|
+
object({ ...shape, ...other })
|
|
29
|
+
.pick('key')
|
|
30
|
+
.toCode('t'),
|
|
31
|
+
).toEqual(['t.object({', ['key: t.string(),'], '})']);
|
|
32
|
+
|
|
33
|
+
expect(
|
|
34
|
+
object({ ...shape, ...other })
|
|
35
|
+
.omit('key')
|
|
36
|
+
.toCode('t'),
|
|
37
|
+
).toEqual(['t.object({', ['other: t.number(),'], '})']);
|
|
38
|
+
|
|
39
|
+
expect(
|
|
40
|
+
object({ ...shape, ...other })
|
|
41
|
+
.partial()
|
|
42
|
+
.toCode('t'),
|
|
43
|
+
).toEqual([
|
|
44
|
+
't.object({',
|
|
45
|
+
['key: t.string().optional(),', 'other: t.number().optional(),'],
|
|
46
|
+
'})',
|
|
47
|
+
]);
|
|
48
|
+
|
|
49
|
+
expect(
|
|
50
|
+
object({ ...shape, ...other })
|
|
51
|
+
.deepPartial()
|
|
52
|
+
.toCode('t'),
|
|
53
|
+
).toEqual([
|
|
54
|
+
't.object({',
|
|
55
|
+
['key: t.string().optional(),', 'other: t.number().optional(),'],
|
|
56
|
+
'})',
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
expect(object(shape).passthrough().toCode('t')).toEqual([
|
|
60
|
+
't.object({',
|
|
61
|
+
['key: t.string(),'],
|
|
62
|
+
'}).passthrough()',
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
expect(object(shape).strict().toCode('t')).toEqual([
|
|
66
|
+
't.object({',
|
|
67
|
+
['key: t.string(),'],
|
|
68
|
+
'}).strict()',
|
|
69
|
+
]);
|
|
70
|
+
|
|
71
|
+
expect(object(shape).catchAll(scalarTypes.string()).toCode('t')).toEqual([
|
|
72
|
+
't.object({',
|
|
73
|
+
['key: t.string(),'],
|
|
74
|
+
'}).catchAll(t.string())',
|
|
75
|
+
]);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
@@ -1,5 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
constructType,
|
|
3
|
+
DeepPartial,
|
|
4
|
+
JSONType,
|
|
5
|
+
JSONTypeAny,
|
|
6
|
+
toCode,
|
|
7
|
+
} from './typeBase';
|
|
2
8
|
import { JSONOptional, optional } from './optional';
|
|
9
|
+
import { Code } from '../columnType';
|
|
3
10
|
|
|
4
11
|
export type JSONObjectShape = Record<string, JSONTypeAny>;
|
|
5
12
|
|
|
@@ -36,7 +43,7 @@ export type baseObjectOutputType<Shape extends JSONObjectShape> = flatten<
|
|
|
36
43
|
}>
|
|
37
44
|
>;
|
|
38
45
|
|
|
39
|
-
type
|
|
46
|
+
type ObjectOutputType<
|
|
40
47
|
Shape extends JSONObjectShape,
|
|
41
48
|
Catchall extends JSONTypeAny,
|
|
42
49
|
> = JSONTypeAny extends Catchall
|
|
@@ -72,7 +79,7 @@ export interface JSONObject<
|
|
|
72
79
|
T extends JSONObjectShape,
|
|
73
80
|
UnknownKeys extends UnknownKeysParam = 'strip',
|
|
74
81
|
Catchall extends JSONTypeAny = JSONTypeAny,
|
|
75
|
-
Output =
|
|
82
|
+
Output = ObjectOutputType<T, Catchall>,
|
|
76
83
|
> extends JSONType<Output, 'object'> {
|
|
77
84
|
shape: T;
|
|
78
85
|
unknownKeys: UnknownKeys;
|
|
@@ -120,6 +127,27 @@ export const object = <
|
|
|
120
127
|
shape,
|
|
121
128
|
unknownKeys: 'strip' as UnknownKeys,
|
|
122
129
|
catchAllType: undefined as unknown as Catchall,
|
|
130
|
+
toCode(this: JSONObject<JSONObjectShape, UnknownKeysParam>, t: string) {
|
|
131
|
+
const { shape } = this;
|
|
132
|
+
const arr: Code[] = [];
|
|
133
|
+
|
|
134
|
+
for (const key in shape) {
|
|
135
|
+
arr.push(`${key}: ${shape[key].toCode(t)},`);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
let lastLine = '})';
|
|
139
|
+
if (this.unknownKeys === 'passthrough') {
|
|
140
|
+
lastLine += '.passthrough()';
|
|
141
|
+
} else if (this.unknownKeys === 'strict') {
|
|
142
|
+
lastLine += '.strict()';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (this.catchAllType) {
|
|
146
|
+
lastLine += `.catchAll(${this.catchAllType.toCode(t)})`;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return toCode(this, t, [`${t}.object({`, arr, lastLine]);
|
|
150
|
+
},
|
|
123
151
|
extend<S extends JSONObjectShape>(add: S) {
|
|
124
152
|
return object<Merge<T, S>, UnknownKeys, Catchall>(
|
|
125
153
|
Object.assign({ ...this.shape }, add),
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { scalarTypes } from './scalarTypes';
|
|
2
|
+
import { optional, required } from './optional';
|
|
3
|
+
|
|
4
|
+
describe('optional', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(optional(scalarTypes.string()).toCode('t')).toBe(
|
|
7
|
+
't.string().optional()',
|
|
8
|
+
);
|
|
9
|
+
});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('required', () => {
|
|
13
|
+
it('should have toCode', () => {
|
|
14
|
+
expect(required(scalarTypes.string().optional()).toCode('t')).toBe(
|
|
15
|
+
't.string()',
|
|
16
|
+
);
|
|
17
|
+
});
|
|
18
|
+
});
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { record } from './record';
|
|
2
|
+
import { scalarTypes } from './scalarTypes';
|
|
3
|
+
|
|
4
|
+
describe('record', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(record(scalarTypes.number()).toCode('t')).toBe(
|
|
7
|
+
't.record(t.number())',
|
|
8
|
+
);
|
|
9
|
+
|
|
10
|
+
expect(record(scalarTypes.number(), scalarTypes.number()).toCode('t')).toBe(
|
|
11
|
+
't.record(t.number(), t.number())',
|
|
12
|
+
);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { constructType, JSONType, JSONTypeAny } from './typeBase';
|
|
1
|
+
import { constructType, JSONType, JSONTypeAny, toCode } from './typeBase';
|
|
2
2
|
import { JSONNumber, JSONString, scalarTypes } from './scalarTypes';
|
|
3
3
|
|
|
4
4
|
export interface JSONRecord<
|
|
@@ -33,6 +33,17 @@ export function record<
|
|
|
33
33
|
dataType: 'record',
|
|
34
34
|
keyType,
|
|
35
35
|
valueType,
|
|
36
|
+
toCode(this: JSONRecord<KeyType, ValueType>, t: string) {
|
|
37
|
+
return toCode(
|
|
38
|
+
this,
|
|
39
|
+
t,
|
|
40
|
+
`${t}.record(${
|
|
41
|
+
args.length === 1
|
|
42
|
+
? this.valueType.toCode(t)
|
|
43
|
+
: `${this.keyType.toCode(t)}, ${this.valueType.toCode(t)}`
|
|
44
|
+
})`,
|
|
45
|
+
);
|
|
46
|
+
},
|
|
36
47
|
deepPartial(this: JSONRecord<KeyType, ValueType>) {
|
|
37
48
|
return {
|
|
38
49
|
...this,
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { scalarTypes } from './scalarTypes';
|
|
2
|
+
|
|
3
|
+
describe('scalarTypes', () => {
|
|
4
|
+
describe('any', () => {
|
|
5
|
+
it('should have toCode', () => {
|
|
6
|
+
expect(scalarTypes.any().toCode('t')).toBe('t.any()');
|
|
7
|
+
});
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
describe('bigint', () => {
|
|
11
|
+
it('should have toCode', () => {
|
|
12
|
+
expect(scalarTypes.bigint().toCode('t')).toBe('t.bigint()');
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
describe('boolean', () => {
|
|
17
|
+
it('should have toCode', () => {
|
|
18
|
+
expect(scalarTypes.boolean().toCode('t')).toBe('t.boolean()');
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
describe('nan', () => {
|
|
23
|
+
it('should have toCode', () => {
|
|
24
|
+
expect(scalarTypes.nan().toCode('t')).toBe('t.nan()');
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
describe('never', () => {
|
|
29
|
+
it('should have toCode', () => {
|
|
30
|
+
expect(scalarTypes.never().toCode('t')).toBe('t.never()');
|
|
31
|
+
});
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
describe('null', () => {
|
|
35
|
+
it('should have toCode', () => {
|
|
36
|
+
expect(scalarTypes.null().toCode('t')).toBe('t.null()');
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('number', () => {
|
|
41
|
+
it('should have toCode', () => {
|
|
42
|
+
expect(scalarTypes.number().toCode('t')).toBe('t.number()');
|
|
43
|
+
|
|
44
|
+
expect(
|
|
45
|
+
scalarTypes
|
|
46
|
+
.number()
|
|
47
|
+
.lt(1)
|
|
48
|
+
.lte(2)
|
|
49
|
+
.gt(3)
|
|
50
|
+
.gte(4)
|
|
51
|
+
.multipleOf(5)
|
|
52
|
+
.int()
|
|
53
|
+
.toCode('t'),
|
|
54
|
+
).toBe('t.number().min(4).gt(3).max(2).lt(1).step(5).int()');
|
|
55
|
+
|
|
56
|
+
expect(
|
|
57
|
+
scalarTypes
|
|
58
|
+
.number()
|
|
59
|
+
.positive()
|
|
60
|
+
.nonNegative()
|
|
61
|
+
.negative()
|
|
62
|
+
.nonPositive()
|
|
63
|
+
.toCode('t'),
|
|
64
|
+
).toBe('t.number().min(0).gt(0).max(0).lt(0)');
|
|
65
|
+
|
|
66
|
+
expect(scalarTypes.number().min(1).max(2).step(3).toCode('t')).toBe(
|
|
67
|
+
't.number().min(1).max(2).step(3)',
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
describe('date', () => {
|
|
73
|
+
it('should have toCode', () => {
|
|
74
|
+
expect(scalarTypes.date().toCode('t')).toBe('t.date()');
|
|
75
|
+
|
|
76
|
+
const now = new Date();
|
|
77
|
+
const s = now.toISOString();
|
|
78
|
+
expect(scalarTypes.date().min(now).max(now).toCode('t')).toBe(
|
|
79
|
+
`t.date().min(new Date('${s}')).max(new Date('${s}'))`,
|
|
80
|
+
);
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
describe('string', () => {
|
|
85
|
+
it('should have toCode', () => {
|
|
86
|
+
expect(scalarTypes.string().toCode('t')).toBe('t.string()');
|
|
87
|
+
|
|
88
|
+
expect(scalarTypes.string().nonEmpty().toCode('t')).toBe(
|
|
89
|
+
't.string().nonEmpty()',
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(
|
|
93
|
+
scalarTypes
|
|
94
|
+
.string()
|
|
95
|
+
.min(1)
|
|
96
|
+
.max(10)
|
|
97
|
+
.length(15)
|
|
98
|
+
.email()
|
|
99
|
+
.url()
|
|
100
|
+
.uuid()
|
|
101
|
+
.cuid()
|
|
102
|
+
.startsWith('start')
|
|
103
|
+
.endsWith('end')
|
|
104
|
+
.trim()
|
|
105
|
+
.toCode('t'),
|
|
106
|
+
).toBe(
|
|
107
|
+
`t.string().min(1).max(10).length(15).email().url().uuid().cuid().startsWith('start').endsWith('end').trim()`,
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
expect(scalarTypes.string().regex(/\d+/g).toCode('t')).toBe(
|
|
111
|
+
`t.string().regex(/\\d+/g)`,
|
|
112
|
+
);
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
describe('undefined', () => {
|
|
117
|
+
it('should have toCode', () => {
|
|
118
|
+
expect(scalarTypes.undefined().toCode('t')).toBe('t.undefined()');
|
|
119
|
+
});
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
describe('unknown', () => {
|
|
123
|
+
it('should have toCode', () => {
|
|
124
|
+
expect(scalarTypes.unknown().toCode('t')).toBe('t.unknown()');
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('void', () => {
|
|
129
|
+
it('should have toCode', () => {
|
|
130
|
+
expect(scalarTypes.void().toCode('t')).toBe('t.void()');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
});
|