prostgles-server 2.0.333 → 2.0.335
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/DBSchemaBuilder.js +4 -4
- package/dist/DBSchemaBuilder.js.map +1 -1
- package/dist/TableConfig.d.ts +21 -10
- package/dist/TableConfig.d.ts.map +1 -1
- package/dist/TableConfig.js +5 -5
- package/dist/TableConfig.js.map +1 -1
- package/dist/validation.d.ts +39 -12
- package/dist/validation.d.ts.map +1 -1
- package/dist/validation.js +82 -78
- package/dist/validation.js.map +1 -1
- package/lib/DBSchemaBuilder.js +4 -4
- package/lib/DBSchemaBuilder.ts +3 -3
- package/lib/TableConfig.d.ts +21 -10
- package/lib/TableConfig.d.ts.map +1 -1
- package/lib/TableConfig.js +5 -5
- package/lib/TableConfig.ts +25 -15
- package/lib/validation.d.ts +39 -12
- package/lib/validation.d.ts.map +1 -1
- package/lib/validation.js +82 -78
- package/lib/validation.ts +188 -146
- package/package.json +1 -1
- package/tests/client/PID.txt +1 -1
- package/tests/isomorphic_queries.js +3 -3
- package/tests/isomorphic_queries.ts +3 -3
- package/tests/server/index.js +10 -10
- package/tests/server/index.ts +10 -10
- package/tests/server/package-lock.json +1 -1
- package/tests/server/server.ts +2 -2
package/lib/DBSchemaBuilder.js
CHANGED
|
@@ -22,10 +22,10 @@ const getDBSchema = (dboBuilder) => {
|
|
|
22
22
|
throw "colConf.jsonbSchema missing";
|
|
23
23
|
type = (0, validation_1.getJSONBSchemaTSTypes)(colConf.jsonbSchema, { nullable: colConf.nullable }, " ");
|
|
24
24
|
}
|
|
25
|
-
else if ((0, prostgles_types_1.isObject)(colConf) && "
|
|
26
|
-
if (!colConf.
|
|
27
|
-
throw "colConf.
|
|
28
|
-
const types = colConf.
|
|
25
|
+
else if ((0, prostgles_types_1.isObject)(colConf) && "enum" in colConf) {
|
|
26
|
+
if (!colConf.enum)
|
|
27
|
+
throw "colConf.enum missing";
|
|
28
|
+
const types = colConf.enum.map(t => typeof t === "number" ? t : JSON.stringify(t));
|
|
29
29
|
if (colConf.nullable) {
|
|
30
30
|
types.unshift("null");
|
|
31
31
|
}
|
package/lib/DBSchemaBuilder.ts
CHANGED
|
@@ -20,9 +20,9 @@ export const getDBSchema = (dboBuilder: DboBuilder): string => {
|
|
|
20
20
|
if(isObject(colConf) && "jsonbSchema" in colConf){
|
|
21
21
|
if(!colConf.jsonbSchema) throw "colConf.jsonbSchema missing"
|
|
22
22
|
type = getJSONBSchemaTSTypes(colConf.jsonbSchema, { nullable: colConf.nullable }, " ");
|
|
23
|
-
} else if(isObject(colConf) && "
|
|
24
|
-
if(!colConf.
|
|
25
|
-
const types = colConf.
|
|
23
|
+
} else if(isObject(colConf) && "enum" in colConf){
|
|
24
|
+
if(!colConf.enum) throw "colConf.enum missing"
|
|
25
|
+
const types = colConf.enum.map(t => typeof t === "number"? t : JSON.stringify(t));
|
|
26
26
|
if(colConf.nullable){
|
|
27
27
|
types.unshift("null")
|
|
28
28
|
}
|
package/lib/TableConfig.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { AnyObject, TableInfo, ALLOWED_EXTENSION, ALLOWED_CONTENT_TYPE } from "prostgles-types";
|
|
2
2
|
import { JoinInfo } from "./DboBuilder";
|
|
3
3
|
import { DB, DBHandlerServer, Prostgles } from "./Prostgles";
|
|
4
|
-
import {
|
|
4
|
+
import { OneOf, ValidationSchema } from "./validation";
|
|
5
5
|
declare type ColExtraInfo = {
|
|
6
6
|
min?: string | number;
|
|
7
7
|
max?: string | number;
|
|
@@ -78,11 +78,11 @@ declare type SQLDefColumn = {
|
|
|
78
78
|
*/
|
|
79
79
|
sqlDefinition?: string;
|
|
80
80
|
};
|
|
81
|
-
declare type
|
|
82
|
-
defaultValue?:
|
|
81
|
+
declare type BaseColumnTypes = {
|
|
82
|
+
defaultValue?: any;
|
|
83
83
|
nullable?: boolean;
|
|
84
84
|
};
|
|
85
|
-
declare type TextColumn =
|
|
85
|
+
declare type TextColumn = BaseColumnTypes & {
|
|
86
86
|
isText: true;
|
|
87
87
|
/**
|
|
88
88
|
* Value will be trimmed before update/insert
|
|
@@ -93,8 +93,8 @@ declare type TextColumn = TextColDef & {
|
|
|
93
93
|
*/
|
|
94
94
|
lowerCased?: boolean;
|
|
95
95
|
};
|
|
96
|
-
export declare type JSONBColumnDef =
|
|
97
|
-
jsonbSchema: ValidationSchema | Omit<
|
|
96
|
+
export declare type JSONBColumnDef = BaseColumnTypes & {
|
|
97
|
+
jsonbSchema: ValidationSchema | Omit<OneOf, "optional">;
|
|
98
98
|
};
|
|
99
99
|
/**
|
|
100
100
|
* Allows referencing media to this table.
|
|
@@ -116,7 +116,7 @@ declare type ReferencedColumn = {
|
|
|
116
116
|
/**
|
|
117
117
|
* Will create a lookup table that this column will reference
|
|
118
118
|
*/
|
|
119
|
-
references?:
|
|
119
|
+
references?: BaseColumnTypes & {
|
|
120
120
|
tableName: string;
|
|
121
121
|
/**
|
|
122
122
|
* Defaults to id
|
|
@@ -136,14 +136,25 @@ declare type NamedJoinColumn = {
|
|
|
136
136
|
label?: string;
|
|
137
137
|
joinDef: JoinDef[];
|
|
138
138
|
};
|
|
139
|
-
declare type
|
|
140
|
-
|
|
139
|
+
declare type Enum<T extends string | number = any> = {
|
|
140
|
+
enum: T[];
|
|
141
141
|
nullable?: boolean;
|
|
142
142
|
defaultValue?: T;
|
|
143
143
|
};
|
|
144
144
|
export declare type ColumnConfig<LANG_IDS = {
|
|
145
145
|
en: 1;
|
|
146
|
-
}> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef |
|
|
146
|
+
}> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef | Enum))>;
|
|
147
|
+
export declare type ColumnConfigs<LANG_IDS = {
|
|
148
|
+
en: 1;
|
|
149
|
+
}> = {
|
|
150
|
+
sql: string | BaseColumn<LANG_IDS> & SQLDefColumn;
|
|
151
|
+
join: BaseColumn<LANG_IDS> & NamedJoinColumn;
|
|
152
|
+
media: BaseColumn<LANG_IDS> & MediaColumn;
|
|
153
|
+
referenced: BaseColumn<LANG_IDS> & ReferencedColumn;
|
|
154
|
+
text: BaseColumn<LANG_IDS> & TextColumn;
|
|
155
|
+
jsonb: BaseColumn<LANG_IDS> & JSONBColumnDef;
|
|
156
|
+
enum: BaseColumn<LANG_IDS> & Enum;
|
|
157
|
+
};
|
|
147
158
|
declare type UnionKeys<T> = T extends T ? keyof T : never;
|
|
148
159
|
declare type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
|
|
149
160
|
declare type StrictUnion<T> = StrictUnionHelper<T, T>;
|
package/lib/TableConfig.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TableConfig.d.ts","sourceRoot":"","sources":["TableConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,SAAS,EAAE,SAAS,EAAG,iBAAiB,EAAE,oBAAoB,EAAY,MAAM,iBAAiB,CAAC;AAC5H,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,EAAE,EAAE,eAAe,EAAS,SAAS,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,EAAoD,
|
|
1
|
+
{"version":3,"file":"TableConfig.d.ts","sourceRoot":"","sources":["TableConfig.ts"],"names":[],"mappings":"AAAA,OAAO,EAAmB,SAAS,EAAE,SAAS,EAAG,iBAAiB,EAAE,oBAAoB,EAAY,MAAM,iBAAiB,CAAC;AAC5H,OAAO,EAAiB,QAAQ,EAAE,MAAM,cAAc,CAAC;AACvD,OAAO,EAAE,EAAE,EAAE,eAAe,EAAS,SAAS,EAAE,MAAM,aAAa,CAAC;AAEpE,OAAO,EAAoD,KAAK,EAAE,gBAAgB,EAAE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CAAC,eAAe,CAAC,QAAQ,CAAC,GAAG,qBAAqB,CAAC,QAAQ,CAAC,CAAC,CAAC;CACrH,CAAA;AAED;;GAEG;AACH,MAAM,CAAC,OAAO,OAAO,iBAAiB,CAAC,QAAQ,GAAG;IAAE,EAAE,EAAE,CAAC,CAAA;CAAE;IAEzD,MAAM,CAAC,EAAE,WAAW,CAAC,QAAQ,CAAC,CAAC;IAC/B,IAAI,GAAG,IAAI,eAAe,CAGzB;IACD,IAAI,EAAE,IAAI,EAAE,CAGX;IAED,SAAS,EAAE,SAAS,CAAA;gBAER,SAAS,EAAE,SAAS;IAKhC,eAAe,cAAe,MAAM,WAAW,MAAM,KAAG,YAAY,GAAG,SAAS,CAM/E;IAED,YAAY,WAAY;QAAE,SAAS,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,KAAG,SAAS,CAAC,MAAM,CAAC,GAAG,SAAS,CAM3F;IAED,UAAU,WAAY;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,IAAI,CAAC,EAAE,MAAM,CAAA;KAAE,KAAG,CAAC,YAAY,GAAG;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,UAAU,CAAC,EAAE,SAAS,CAAC;KAAE,CAAC,GAAG,SAAS,CAqC7I;IAED,WAAW,WAAY;QAAE,GAAG,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,GAAG,CAAA;KAAE,KAAG,IAAI,CAQvE;IAED,WAAW,gBAAiB,MAAM,eAAe,MAAM,KAAG,QAAQ,GAAG,SAAS,CA4B7E;IAEK,IAAI;CA8OX"}
|
package/lib/TableConfig.js
CHANGED
|
@@ -205,11 +205,11 @@ class TableConfigurator {
|
|
|
205
205
|
const checkStatement = (0, validation_1.getPGCheckConstraint)({ schema: colConf.jsonbSchema, escapedFieldName: colNameEsc, nullable: !!colConf.nullable }, 0);
|
|
206
206
|
return ` ${colNameEsc} ${getColTypeDef(colConf, "JSONB")} CHECK(${checkStatement})`;
|
|
207
207
|
}
|
|
208
|
-
else if ("
|
|
209
|
-
if (!colConf.
|
|
210
|
-
throw new Error("colConf.
|
|
211
|
-
const type = colConf.
|
|
212
|
-
const checks = colConf.
|
|
208
|
+
else if ("enum" in colConf) {
|
|
209
|
+
if (!colConf.enum?.length)
|
|
210
|
+
throw new Error("colConf.enum Must not be empty");
|
|
211
|
+
const type = colConf.enum.every(v => Number.isFinite(v)) ? "NUMERIC" : "TEXT";
|
|
212
|
+
const checks = colConf.enum.map(v => `${colNameEsc} = ${(0, PubSubManager_1.asValue)(v)}`).join(" OR ");
|
|
213
213
|
return ` ${colNameEsc} ${type} ${colConf.nullable ? "" : "NOT NULL"} ${"defaultValue" in colConf ? ` DEFAULT ${(0, PubSubManager_1.asValue)(colConf.defaultValue)}` : ""} CHECK(${checks})`;
|
|
214
214
|
}
|
|
215
215
|
else {
|
package/lib/TableConfig.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { getKeys, asName, AnyObject, TableInfo, ALLOWED_EXTENSION, ALLOWED_CONT
|
|
|
2
2
|
import { isPlainObject, JoinInfo } from "./DboBuilder";
|
|
3
3
|
import { DB, DBHandlerServer, Joins, Prostgles } from "./Prostgles";
|
|
4
4
|
import { asValue } from "./PubSubManager";
|
|
5
|
-
import { getJSONBSchemaAsJSONSchema, getPGCheckConstraint,
|
|
5
|
+
import { getJSONBSchemaAsJSONSchema, getPGCheckConstraint, OneOf, ValidationSchema } from "./validation";
|
|
6
6
|
|
|
7
7
|
type ColExtraInfo = {
|
|
8
8
|
min?: string | number;
|
|
@@ -99,12 +99,12 @@ type SQLDefColumn = {
|
|
|
99
99
|
sqlDefinition?: string;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
-
type
|
|
103
|
-
defaultValue?:
|
|
102
|
+
type BaseColumnTypes = {
|
|
103
|
+
defaultValue?: any;
|
|
104
104
|
nullable?: boolean;
|
|
105
105
|
}
|
|
106
106
|
|
|
107
|
-
type TextColumn =
|
|
107
|
+
type TextColumn = BaseColumnTypes & {
|
|
108
108
|
isText: true;
|
|
109
109
|
/**
|
|
110
110
|
* Value will be trimmed before update/insert
|
|
@@ -117,8 +117,8 @@ type TextColumn = TextColDef & {
|
|
|
117
117
|
lowerCased?: boolean;
|
|
118
118
|
}
|
|
119
119
|
|
|
120
|
-
export type JSONBColumnDef =
|
|
121
|
-
jsonbSchema: ValidationSchema | Omit<
|
|
120
|
+
export type JSONBColumnDef = BaseColumnTypes & {
|
|
121
|
+
jsonbSchema: ValidationSchema | Omit<OneOf, "optional">;
|
|
122
122
|
|
|
123
123
|
/**
|
|
124
124
|
* If the new schema CHECK fails old rows the update old rows using this function
|
|
@@ -153,7 +153,7 @@ type ReferencedColumn = {
|
|
|
153
153
|
/**
|
|
154
154
|
* Will create a lookup table that this column will reference
|
|
155
155
|
*/
|
|
156
|
-
references?:
|
|
156
|
+
references?: BaseColumnTypes & {
|
|
157
157
|
|
|
158
158
|
|
|
159
159
|
tableName: string;
|
|
@@ -179,13 +179,23 @@ type NamedJoinColumn = {
|
|
|
179
179
|
joinDef: JoinDef[];
|
|
180
180
|
}
|
|
181
181
|
|
|
182
|
-
type
|
|
183
|
-
|
|
182
|
+
type Enum<T extends string | number = any> = {
|
|
183
|
+
enum: T[];
|
|
184
184
|
nullable?: boolean;
|
|
185
185
|
defaultValue?: T;
|
|
186
186
|
};
|
|
187
187
|
|
|
188
|
-
export type ColumnConfig<LANG_IDS = { en: 1 }> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef |
|
|
188
|
+
export type ColumnConfig<LANG_IDS = { en: 1 }> = string | StrictUnion<NamedJoinColumn | MediaColumn | (BaseColumn<LANG_IDS> & (SQLDefColumn | ReferencedColumn | TextColumn | JSONBColumnDef | Enum))>;
|
|
189
|
+
|
|
190
|
+
export type ColumnConfigs<LANG_IDS = { en: 1 }> = {
|
|
191
|
+
sql: string | BaseColumn<LANG_IDS> & SQLDefColumn;
|
|
192
|
+
join: BaseColumn<LANG_IDS> & NamedJoinColumn;
|
|
193
|
+
media: BaseColumn<LANG_IDS> & MediaColumn;
|
|
194
|
+
referenced: BaseColumn<LANG_IDS> & ReferencedColumn;
|
|
195
|
+
text: BaseColumn<LANG_IDS> & TextColumn;
|
|
196
|
+
jsonb: BaseColumn<LANG_IDS> & JSONBColumnDef;
|
|
197
|
+
enum: BaseColumn<LANG_IDS> & Enum;
|
|
198
|
+
}
|
|
189
199
|
|
|
190
200
|
type UnionKeys<T> = T extends T ? keyof T : never;
|
|
191
201
|
type StrictUnionHelper<T, TAll> = T extends any ? T & Partial<Record<Exclude<UnionKeys<TAll>, keyof T>, never>> : never;
|
|
@@ -416,7 +426,7 @@ export default class TableConfigurator<LANG_IDS = { en: 1 }> {
|
|
|
416
426
|
if ("columns" in tableConf) {
|
|
417
427
|
const getColDef = (name: string, colConf: ColumnConfig): string => {
|
|
418
428
|
const colNameEsc = asName(name);
|
|
419
|
-
const getColTypeDef = (colConf:
|
|
429
|
+
const getColTypeDef = (colConf: BaseColumnTypes, pgType: "TEXT" | "JSONB") => {
|
|
420
430
|
const { nullable, defaultValue } = colConf;
|
|
421
431
|
return `${pgType} ${!nullable ? " NOT NULL " : ""} ${defaultValue ? ` DEFAULT ${asValue(defaultValue)} ` : ""}`
|
|
422
432
|
}
|
|
@@ -458,10 +468,10 @@ export default class TableConfigurator<LANG_IDS = { en: 1 }> {
|
|
|
458
468
|
const checkStatement = getPGCheckConstraint({ schema: colConf.jsonbSchema, escapedFieldName: colNameEsc, nullable: !!colConf.nullable }, 0)
|
|
459
469
|
return ` ${colNameEsc} ${getColTypeDef(colConf, "JSONB")} CHECK(${checkStatement})`;
|
|
460
470
|
|
|
461
|
-
} else if("
|
|
462
|
-
if(!colConf.
|
|
463
|
-
const type = colConf.
|
|
464
|
-
const checks = colConf.
|
|
471
|
+
} else if("enum" in colConf) {
|
|
472
|
+
if(!colConf.enum?.length) throw new Error("colConf.enum Must not be empty");
|
|
473
|
+
const type = colConf.enum.every(v => Number.isFinite(v))? "NUMERIC" : "TEXT";
|
|
474
|
+
const checks = colConf.enum.map(v => `${colNameEsc} = ${asValue(v)}`).join(" OR ");
|
|
465
475
|
return ` ${colNameEsc} ${type} ${colConf.nullable? "" : "NOT NULL"} ${"defaultValue" in colConf? ` DEFAULT ${asValue(colConf.defaultValue)}` : ""} CHECK(${checks})`;
|
|
466
476
|
|
|
467
477
|
} else {
|
package/lib/validation.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AnyObject } from "prostgles-types";
|
|
2
1
|
import { BaseColumn, JSONBColumnDef } from "./TableConfig";
|
|
3
2
|
declare type BaseOptions = {
|
|
4
3
|
optional?: boolean;
|
|
@@ -9,12 +8,12 @@ declare type BaseOptions = {
|
|
|
9
8
|
declare type SimpleType = BaseOptions & ({
|
|
10
9
|
type: "number" | "boolean" | "integer" | "string" | "any" | "number[]" | "boolean[]" | "integer[]" | "string[]" | "any[]" | ValidationSchema;
|
|
11
10
|
} | {
|
|
12
|
-
|
|
11
|
+
enum: readonly any[];
|
|
13
12
|
});
|
|
14
|
-
export declare type
|
|
15
|
-
|
|
13
|
+
export declare type OneOf = BaseOptions & {
|
|
14
|
+
oneOf: readonly ValidationSchema[];
|
|
16
15
|
};
|
|
17
|
-
declare type FieldType = SimpleType |
|
|
16
|
+
declare type FieldType = SimpleType | OneOf;
|
|
18
17
|
declare type GetType<T extends FieldType> = T extends {
|
|
19
18
|
type: ValidationSchema;
|
|
20
19
|
} ? SchemaObject<T["type"]> : T extends {
|
|
@@ -38,12 +37,12 @@ declare type GetType<T extends FieldType> = T extends {
|
|
|
38
37
|
} ? string[] : T extends {
|
|
39
38
|
type: "any[]";
|
|
40
39
|
} ? any[] : T extends {
|
|
41
|
-
|
|
42
|
-
} ? T["
|
|
40
|
+
enum: readonly any[];
|
|
41
|
+
} ? T["enum"][number] :
|
|
43
42
|
/** This needs fixing */
|
|
44
43
|
T extends {
|
|
45
|
-
|
|
46
|
-
} ? SchemaObject<T["
|
|
44
|
+
oneOf: readonly ValidationSchema[];
|
|
45
|
+
} ? SchemaObject<T["oneOf"][number]> : any;
|
|
47
46
|
export declare type ValidationSchema = Record<string, FieldType>;
|
|
48
47
|
export declare type SchemaObject<S extends ValidationSchema> = ({
|
|
49
48
|
[K in keyof S as S[K]["optional"] extends true ? K : never]?: GetType<S[K]>;
|
|
@@ -54,7 +53,7 @@ export declare function validate<T>(obj: T, key: keyof T, validation: FieldType)
|
|
|
54
53
|
export declare function validateSchema<S extends ValidationSchema>(schema: S, obj: SchemaObject<S>, objName?: string, optional?: boolean): void;
|
|
55
54
|
export declare function getPGCheckConstraint(args: {
|
|
56
55
|
escapedFieldName: string;
|
|
57
|
-
schema: ValidationSchema |
|
|
56
|
+
schema: ValidationSchema | OneOf;
|
|
58
57
|
nullable: boolean;
|
|
59
58
|
isRootQuery?: boolean;
|
|
60
59
|
optional?: boolean;
|
|
@@ -63,9 +62,37 @@ declare type ColOpts = {
|
|
|
63
62
|
nullable?: boolean;
|
|
64
63
|
};
|
|
65
64
|
export declare function getSchemaTSTypes(schema: ValidationSchema, leading?: string, isOneOf?: boolean): string;
|
|
66
|
-
export declare function getJSONBSchemaTSTypes(schema: ValidationSchema |
|
|
65
|
+
export declare function getJSONBSchemaTSTypes(schema: ValidationSchema | OneOf, colOpts: ColOpts, leading?: string, isOneOf?: boolean): string;
|
|
66
|
+
declare namespace JSTypes {
|
|
67
|
+
type Base = {
|
|
68
|
+
$id?: string;
|
|
69
|
+
$schema?: string;
|
|
70
|
+
title?: string;
|
|
71
|
+
description?: string;
|
|
72
|
+
required?: boolean;
|
|
73
|
+
};
|
|
74
|
+
export type Any = {};
|
|
75
|
+
export type Object = Base & {
|
|
76
|
+
type: "object";
|
|
77
|
+
properties: Record<string, Schema>;
|
|
78
|
+
};
|
|
79
|
+
export type Enum = Base & {
|
|
80
|
+
type: "string" | "number";
|
|
81
|
+
enum: (string | number)[];
|
|
82
|
+
};
|
|
83
|
+
export type Array = Base & {
|
|
84
|
+
type: "array";
|
|
85
|
+
items: (string | number)[];
|
|
86
|
+
};
|
|
87
|
+
export type OneOf = {
|
|
88
|
+
oneOf: (Object | Enum | Array | Any)[];
|
|
89
|
+
};
|
|
90
|
+
export type Schema = Any | Object | Enum | Array | OneOf;
|
|
91
|
+
export {};
|
|
92
|
+
}
|
|
93
|
+
declare type JSONSchema = JSTypes.Schema;
|
|
67
94
|
export declare function getJSONBSchemaAsJSONSchema(tableName: string, colName: string, columnConfig: BaseColumn<{
|
|
68
95
|
en: 1;
|
|
69
|
-
}> & JSONBColumnDef):
|
|
96
|
+
}> & JSONBColumnDef): JSONSchema;
|
|
70
97
|
export {};
|
|
71
98
|
//# sourceMappingURL=validation.d.ts.map
|
package/lib/validation.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["validation.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"validation.d.ts","sourceRoot":"","sources":["validation.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,UAAU,EAAgB,cAAc,EAAE,MAAM,eAAe,CAAC;AAEzE,aAAK,WAAW,GAAG;IACjB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,aAAK,UAAU,GAAG,WAAW,GAAG,CAAC;IAC/B,IAAI,EACF,QAAQ,GAAG,SAAS,GAAG,SAAS,GAAG,QAAQ,GAAG,KAAK,GACnD,UAAU,GAAG,WAAW,GAAG,WAAW,GAAG,UAAU,GAAG,OAAO,GAC7D,gBAAgB,CAAC;CAEpB,GAAG;IACF,IAAI,EAAE,SAAS,GAAG,EAAE,CAAC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0B,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,UAAU,CAAC;IAAE,EAAE,EAAE,CAAC,CAAA;CAAE,CAAC,GAAG,cAAc,GAAG,UAAU,CAkB/I"}
|
package/lib/validation.js
CHANGED
|
@@ -7,10 +7,12 @@ const PubSubManager_1 = require("./PubSubManager");
|
|
|
7
7
|
const s = {
|
|
8
8
|
a: { type: "boolean" },
|
|
9
9
|
c: { type: { c1: { type: "string" } } },
|
|
10
|
-
o: {
|
|
10
|
+
o: {
|
|
11
|
+
oneOf: [
|
|
11
12
|
{ z: { type: "integer" } },
|
|
12
13
|
{ z1: { type: "integer" } }
|
|
13
|
-
]
|
|
14
|
+
]
|
|
15
|
+
}
|
|
14
16
|
};
|
|
15
17
|
const ss = {
|
|
16
18
|
a: true,
|
|
@@ -38,9 +40,9 @@ function validate(obj, key, validation) {
|
|
|
38
40
|
if (validation.type === "integer" && !Number.isInteger(val))
|
|
39
41
|
throw new Error(err);
|
|
40
42
|
}
|
|
41
|
-
else if ("
|
|
42
|
-
err += `on of: ${validation.
|
|
43
|
-
if (!validation.
|
|
43
|
+
else if ("enum" in validation && validation.enum) {
|
|
44
|
+
err += `on of: ${validation.enum}`;
|
|
45
|
+
if (!validation.enum.includes(val))
|
|
44
46
|
throw new Error(err);
|
|
45
47
|
}
|
|
46
48
|
return true;
|
|
@@ -70,18 +72,18 @@ function getPGCheckConstraint(args, depth) {
|
|
|
70
72
|
checks.push(`${valAsJson} IS NULL`);
|
|
71
73
|
if (t.optional)
|
|
72
74
|
checks.push(`${escapedFieldName} ? ${(0, PubSubManager_1.asValue)(k)} = FALSE`);
|
|
73
|
-
if ("
|
|
74
|
-
checks.push(`(${t.
|
|
75
|
+
if ("oneOf" in t) {
|
|
76
|
+
checks.push(`(${t.oneOf.map(subType => getPGCheckConstraint({ escapedFieldName: valAsJson, schema: subType, nullable, optional: t.optional }, depth + 1)).join(" OR ")})`);
|
|
75
77
|
}
|
|
76
|
-
else if ("
|
|
77
|
-
if (!t.
|
|
78
|
-
throw new Error(`Invalid ValidationSchema for property: ${k} of field ${escapedFieldName}:
|
|
78
|
+
else if ("enum" in t) {
|
|
79
|
+
if (!t.enum.length || t.enum.some(v => v === undefined || !["number", "boolean", "string", null].includes(typeof v))) {
|
|
80
|
+
throw new Error(`Invalid ValidationSchema for property: ${k} of field ${escapedFieldName}: enum cannot be empty AND can only contain: numbers, text, boolean, null`);
|
|
79
81
|
}
|
|
80
|
-
const oneOfHasNull = t.
|
|
82
|
+
const oneOfHasNull = t.enum.includes(null);
|
|
81
83
|
if (oneOfHasNull)
|
|
82
84
|
checks.push(`${valAsText} IS NULL`);
|
|
83
|
-
const
|
|
84
|
-
|
|
85
|
+
const _enum = t.enum.filter(o => o !== null);
|
|
86
|
+
_enum.forEach(o => {
|
|
85
87
|
checks.push(`(${valAsText})${jsToPGtypes[typeof o]} = ${(0, PubSubManager_1.asValue)(o)}`);
|
|
86
88
|
});
|
|
87
89
|
}
|
|
@@ -114,7 +116,7 @@ function getPGCheckConstraint(args, depth) {
|
|
|
114
116
|
const checks = [];
|
|
115
117
|
let typeChecks = "";
|
|
116
118
|
if (isOneOfTypes(s)) {
|
|
117
|
-
typeChecks = s.
|
|
119
|
+
typeChecks = s.oneOf.map(t => `(${getSchemaChecks(t)})`).join(" OR ");
|
|
118
120
|
}
|
|
119
121
|
else {
|
|
120
122
|
typeChecks = getSchemaChecks(s);
|
|
@@ -126,9 +128,9 @@ function getPGCheckConstraint(args, depth) {
|
|
|
126
128
|
}
|
|
127
129
|
exports.getPGCheckConstraint = getPGCheckConstraint;
|
|
128
130
|
const isOneOfTypes = (s) => {
|
|
129
|
-
if ("
|
|
130
|
-
if (!Array.isArray(s.
|
|
131
|
-
throw "Expecting
|
|
131
|
+
if ("oneOf" in s) {
|
|
132
|
+
if (!Array.isArray(s.oneOf)) {
|
|
133
|
+
throw "Expecting oneOf to be an array of types";
|
|
132
134
|
}
|
|
133
135
|
return true;
|
|
134
136
|
}
|
|
@@ -146,11 +148,11 @@ function getSchemaTSTypes(schema, leading = "", isOneOf = false) {
|
|
|
146
148
|
return nullType + getSchemaTSTypes(def.type, "", true);
|
|
147
149
|
}
|
|
148
150
|
}
|
|
149
|
-
else if ("
|
|
150
|
-
return nullType + def.
|
|
151
|
+
else if ("enum" in def) {
|
|
152
|
+
return nullType + def.enum.map(v => (0, PubSubManager_1.asValue)(v)).join(" | ");
|
|
151
153
|
}
|
|
152
|
-
else if ("
|
|
153
|
-
return (def.nullable ? `\n${leading} | null` : "") + def.
|
|
154
|
+
else if ("oneOf" in def) {
|
|
155
|
+
return (def.nullable ? `\n${leading} | null` : "") + def.oneOf.map(v => `\n${leading} | ` + getSchemaTSTypes(v, "", true)).join("");
|
|
154
156
|
}
|
|
155
157
|
else
|
|
156
158
|
throw "Unexpected getSchemaTSTypes";
|
|
@@ -168,82 +170,84 @@ function getSchemaTSTypes(schema, leading = "", isOneOf = false) {
|
|
|
168
170
|
exports.getSchemaTSTypes = getSchemaTSTypes;
|
|
169
171
|
function getJSONBSchemaTSTypes(schema, colOpts, leading = "", isOneOf = false) {
|
|
170
172
|
if (isOneOfTypes(schema)) {
|
|
171
|
-
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.
|
|
173
|
+
return (colOpts.nullable ? `\n${leading} | null` : "") + schema.oneOf.map(s => `\n${leading} | ` + getSchemaTSTypes(s, "", true)).join("");
|
|
172
174
|
}
|
|
173
175
|
else {
|
|
174
176
|
return (colOpts.nullable ? `null | ` : "") + getSchemaTSTypes(schema, leading, isOneOf);
|
|
175
177
|
}
|
|
176
178
|
}
|
|
177
179
|
exports.getJSONBSchemaTSTypes = getJSONBSchemaTSTypes;
|
|
178
|
-
// type JSONSchema =
|
|
179
180
|
const getJSONSchemaObject = (objDef) => {
|
|
180
|
-
const resultType = {
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
181
|
+
const resultType = {
|
|
182
|
+
type: "object",
|
|
183
|
+
properties: (0, prostgles_types_1.getKeys)(objDef).reduce((a, k) => {
|
|
184
|
+
const itemSchema = objDef[k];
|
|
185
|
+
const { nullable, optional, description, title } = itemSchema;
|
|
186
|
+
let item = {};
|
|
187
|
+
if ("type" in itemSchema) {
|
|
188
|
+
const { type } = itemSchema;
|
|
189
|
+
/**
|
|
190
|
+
* Is primitive or any
|
|
191
|
+
*/
|
|
192
|
+
if (typeof type === "string") {
|
|
193
|
+
const arrayType = type.endsWith("[]") ? type.slice(0, -2) : undefined;
|
|
194
|
+
if (arrayType) {
|
|
195
|
+
item = {
|
|
196
|
+
type: "array",
|
|
197
|
+
items: { type: arrayType === "any" ? {} : arrayType }
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
else {
|
|
201
|
+
item = {
|
|
202
|
+
type: type === "any" ? {} : type
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Is object
|
|
207
|
+
*/
|
|
197
208
|
}
|
|
198
209
|
else {
|
|
199
210
|
item = {
|
|
200
|
-
type:
|
|
211
|
+
type: "object",
|
|
212
|
+
properties: getJSONSchemaObject(type)
|
|
201
213
|
};
|
|
202
214
|
}
|
|
203
|
-
/**
|
|
204
|
-
* Is object
|
|
205
|
-
*/
|
|
206
215
|
}
|
|
207
|
-
else {
|
|
216
|
+
else if ("enum" in itemSchema) {
|
|
208
217
|
item = {
|
|
209
|
-
type:
|
|
210
|
-
|
|
218
|
+
type: typeof itemSchema.enum[0],
|
|
219
|
+
"enum": itemSchema.enum //.concat(nullable? [null] : [])
|
|
211
220
|
};
|
|
212
221
|
}
|
|
213
|
-
|
|
214
|
-
else if ("oneOf" in itemSchema) {
|
|
215
|
-
item = {
|
|
216
|
-
type: typeof itemSchema.oneOf[0],
|
|
217
|
-
"enum": itemSchema.oneOf //.concat(nullable? [null] : [])
|
|
218
|
-
};
|
|
219
|
-
}
|
|
220
|
-
else if ("oneOfTypes" in itemSchema) {
|
|
221
|
-
item = {
|
|
222
|
-
oneOf: itemSchema.oneOfTypes.map(t => getJSONSchemaObject(t))
|
|
223
|
-
};
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
throw new Error("Unexpected jsonbSchema itemSchema" + JSON.stringify({ itemSchema, objDef }, null, 2));
|
|
227
|
-
}
|
|
228
|
-
if (nullable) {
|
|
229
|
-
const nullDef = { type: "null" };
|
|
230
|
-
if (item.oneOf)
|
|
231
|
-
item.oneOf.push(nullDef);
|
|
232
|
-
else
|
|
222
|
+
else if ("oneOf" in itemSchema) {
|
|
233
223
|
item = {
|
|
234
|
-
oneOf:
|
|
224
|
+
oneOf: itemSchema.oneOf.map(t => getJSONSchemaObject(t))
|
|
235
225
|
};
|
|
236
|
-
}
|
|
237
|
-
return {
|
|
238
|
-
...a,
|
|
239
|
-
[k]: {
|
|
240
|
-
...item,
|
|
241
|
-
required: !optional,
|
|
242
|
-
description,
|
|
243
|
-
title,
|
|
244
226
|
}
|
|
245
|
-
|
|
246
|
-
|
|
227
|
+
else {
|
|
228
|
+
throw new Error("Unexpected jsonbSchema itemSchema" + JSON.stringify({ itemSchema, objDef }, null, 2));
|
|
229
|
+
}
|
|
230
|
+
if (nullable) {
|
|
231
|
+
const nullDef = { type: "null" };
|
|
232
|
+
if (item.oneOf)
|
|
233
|
+
item.oneOf.push(nullDef);
|
|
234
|
+
else
|
|
235
|
+
item = {
|
|
236
|
+
oneOf: [item, nullDef]
|
|
237
|
+
};
|
|
238
|
+
}
|
|
239
|
+
return {
|
|
240
|
+
...a,
|
|
241
|
+
[k]: {
|
|
242
|
+
...item,
|
|
243
|
+
required: !optional,
|
|
244
|
+
description,
|
|
245
|
+
title,
|
|
246
|
+
}
|
|
247
|
+
};
|
|
248
|
+
}, {})
|
|
249
|
+
};
|
|
250
|
+
return resultType;
|
|
247
251
|
};
|
|
248
252
|
function getJSONBSchemaAsJSONSchema(tableName, colName, columnConfig) {
|
|
249
253
|
const schema = columnConfig.jsonbSchema;
|