@postxl/schema 2.2.0 → 2.3.0
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/field/field.d.ts +16 -126
- package/dist/field/field.js +52 -8
- package/dist/field/json.d.ts +8 -0
- package/dist/field/json.js +11 -0
- package/dist/field/scalar.d.ts +14 -0
- package/dist/field/scalar.js +73 -2
- package/dist/field/shared/decoders.d.ts +2 -0
- package/dist/field/shared/decoders.js +11 -1
- package/package.json +1 -1
package/dist/field/field.d.ts
CHANGED
|
@@ -9,21 +9,21 @@ import { FieldRelation, FieldRelationEnriched } from './relation';
|
|
|
9
9
|
import { FieldScalar, FieldScalarEnriched } from './scalar';
|
|
10
10
|
/**
|
|
11
11
|
* JSON Schema for a field
|
|
12
|
+
*
|
|
13
|
+
* Rather than offering every branch to a plain `z.union`, this dispatches on `name` and
|
|
14
|
+
* `type` using the rules documented above, and reports only the errors of the branch the
|
|
15
|
+
* field actually belongs to. A union reports every branch's failure, so a scalar field
|
|
16
|
+
* carrying one bad key used to fail with `Model Int is not defined in the project!` and
|
|
17
|
+
* `Enum Int is not defined in the project!` - the last two branches complaining that a
|
|
18
|
+
* database type is not a model or an enum, while the real cause went unmentioned (#830).
|
|
19
|
+
*
|
|
20
|
+
* The dispatch order mirrors the old union order, so which branch wins for any input that
|
|
21
|
+
* decodes successfully is unchanged.
|
|
12
22
|
*/
|
|
13
23
|
export declare const createFieldJSONDecoder: (params: {
|
|
14
24
|
modelNames: Model.ModelName[];
|
|
15
25
|
enumNames: Enum.EnumName[];
|
|
16
|
-
}) => z.
|
|
17
|
-
name: z.ZodLiteral<"id">;
|
|
18
|
-
type: z.ZodUnion<readonly [z.ZodLiteral<"String">, z.ZodLiteral<"Int">]>;
|
|
19
|
-
databaseName: z.ZodOptional<z.ZodString>;
|
|
20
|
-
excelName: z.ZodOptional<z.ZodString>;
|
|
21
|
-
label: z.ZodOptional<z.ZodString>;
|
|
22
|
-
description: z.ZodOptional<z.ZodString>;
|
|
23
|
-
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
24
|
-
faker: z.ZodOptional<z.ZodString>;
|
|
25
|
-
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
26
|
-
}, z.core.$strip>, z.ZodTransform<{
|
|
26
|
+
}) => z.ZodPipe<z.ZodUnknown, z.ZodTransform<{
|
|
27
27
|
kind: "id";
|
|
28
28
|
name: "id" & z.core.$brand<"PXL.FieldName"> & z.core.$brand<"PXL.FieldName.Id">;
|
|
29
29
|
unbrandedTypeName: ("string" & z.core.$brand<"PXL.ScalarIdType"> & z.core.$brand<"PXL.ScalarType"> & z.core.$brand<"PXL.Type">) | ("number" & z.core.$brand<"PXL.ScalarIdType"> & z.core.$brand<"PXL.ScalarType"> & z.core.$brand<"PXL.Type">);
|
|
@@ -39,89 +39,7 @@ export declare const createFieldJSONDecoder: (params: {
|
|
|
39
39
|
isCreatedAt: false;
|
|
40
40
|
isUpdatedAt: false;
|
|
41
41
|
seed: import(".").FieldSeedConfig;
|
|
42
|
-
}
|
|
43
|
-
name: "id";
|
|
44
|
-
type: "String" | "Int";
|
|
45
|
-
databaseName?: string | undefined;
|
|
46
|
-
excelName?: string | undefined;
|
|
47
|
-
label?: string | undefined;
|
|
48
|
-
description?: string | undefined;
|
|
49
|
-
isReadonly?: boolean | undefined;
|
|
50
|
-
faker?: string | undefined;
|
|
51
|
-
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
52
|
-
}>>, z.ZodPipe<z.ZodObject<{
|
|
53
|
-
name: z.ZodString;
|
|
54
|
-
type: z.ZodUnion<readonly [z.ZodUnion<[z.ZodLiteral<"String">, z.ZodLiteral<"String?">]>, z.ZodUnion<[z.ZodLiteral<"Boolean">, z.ZodLiteral<"Boolean?">]>, z.ZodUnion<[z.ZodLiteral<"DateTime">, z.ZodLiteral<"DateTime?">]>, z.ZodUnion<[z.ZodLiteral<"Decimal">, z.ZodLiteral<"Decimal?">]>, z.ZodUnion<[z.ZodLiteral<"Float">, z.ZodLiteral<"Float?">]>, z.ZodUnion<[z.ZodLiteral<"Int">, z.ZodLiteral<"Int?">]>, z.ZodUnion<[z.ZodLiteral<"Bytes">, z.ZodLiteral<"Bytes?">]>, z.ZodUnion<[z.ZodLiteral<"BigInt">, z.ZodLiteral<"BigInt?">]>]>;
|
|
55
|
-
databaseName: z.ZodOptional<z.ZodString>;
|
|
56
|
-
excelName: z.ZodOptional<z.ZodString>;
|
|
57
|
-
label: z.ZodOptional<z.ZodString>;
|
|
58
|
-
description: z.ZodOptional<z.ZodString>;
|
|
59
|
-
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
60
|
-
isUnique: z.ZodOptional<z.ZodBoolean>;
|
|
61
|
-
hasIndex: z.ZodOptional<z.ZodBoolean>;
|
|
62
|
-
isCreatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
63
|
-
isUpdatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
64
|
-
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
65
|
-
faker: z.ZodOptional<z.ZodString>;
|
|
66
|
-
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
67
|
-
placeholder: z.ZodOptional<z.ZodString>;
|
|
68
|
-
defaultValue: z.ZodOptional<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
|
|
69
|
-
}, z.core.$strict>, z.ZodTransform<FieldScalarEnriched, {
|
|
70
|
-
name: string;
|
|
71
|
-
type: "BigInt" | "String" | "Boolean" | "DateTime" | "Decimal" | "Float" | "Int" | "Bytes" | "String?" | "Int?" | "Boolean?" | "DateTime?" | "Decimal?" | "Float?" | "Bytes?" | "BigInt?";
|
|
72
|
-
databaseName?: string | undefined;
|
|
73
|
-
excelName?: string | undefined;
|
|
74
|
-
label?: string | undefined;
|
|
75
|
-
description?: string | undefined;
|
|
76
|
-
isReadonly?: boolean | undefined;
|
|
77
|
-
isUnique?: boolean | undefined;
|
|
78
|
-
hasIndex?: boolean | undefined;
|
|
79
|
-
isCreatedAt?: boolean | undefined;
|
|
80
|
-
isUpdatedAt?: boolean | undefined;
|
|
81
|
-
maxLength?: number | undefined;
|
|
82
|
-
faker?: string | undefined;
|
|
83
|
-
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
84
|
-
placeholder?: string | undefined;
|
|
85
|
-
defaultValue?: string | number | boolean | undefined;
|
|
86
|
-
}>>, z.ZodPipe<z.ZodObject<{
|
|
87
|
-
name: z.ZodString;
|
|
88
|
-
type: z.ZodUnion<readonly [z.ZodLiteral<"Json">, z.ZodLiteral<"JSON">, z.ZodLiteral<"Json?">, z.ZodLiteral<"JSON?">]>;
|
|
89
|
-
databaseName: z.ZodOptional<z.ZodString>;
|
|
90
|
-
excelName: z.ZodOptional<z.ZodString>;
|
|
91
|
-
label: z.ZodOptional<z.ZodString>;
|
|
92
|
-
description: z.ZodOptional<z.ZodString>;
|
|
93
|
-
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
94
|
-
isUnique: z.ZodOptional<z.ZodBoolean>;
|
|
95
|
-
hasIndex: z.ZodOptional<z.ZodBoolean>;
|
|
96
|
-
placeholder: z.ZodOptional<z.ZodString>;
|
|
97
|
-
}, z.core.$strict>, z.ZodTransform<FieldJsonEnriched, {
|
|
98
|
-
name: string;
|
|
99
|
-
type: "Json" | "JSON" | "Json?" | "JSON?";
|
|
100
|
-
databaseName?: string | undefined;
|
|
101
|
-
excelName?: string | undefined;
|
|
102
|
-
label?: string | undefined;
|
|
103
|
-
description?: string | undefined;
|
|
104
|
-
isReadonly?: boolean | undefined;
|
|
105
|
-
isUnique?: boolean | undefined;
|
|
106
|
-
hasIndex?: boolean | undefined;
|
|
107
|
-
placeholder?: string | undefined;
|
|
108
|
-
}>>, z.ZodPipe<z.ZodObject<{
|
|
109
|
-
name: z.ZodString;
|
|
110
|
-
type: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
111
|
-
databaseName: z.ZodOptional<z.ZodString>;
|
|
112
|
-
excelName: z.ZodOptional<z.ZodString>;
|
|
113
|
-
prismaRelationFieldName: z.ZodOptional<z.ZodString>;
|
|
114
|
-
deepClone: z.ZodOptional<z.ZodBoolean>;
|
|
115
|
-
onDelete: z.ZodOptional<z.ZodEnum<{
|
|
116
|
-
cascade: "cascade";
|
|
117
|
-
setNull: "setNull";
|
|
118
|
-
restrict: "restrict";
|
|
119
|
-
}>>;
|
|
120
|
-
label: z.ZodOptional<z.ZodString>;
|
|
121
|
-
description: z.ZodOptional<z.ZodString>;
|
|
122
|
-
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
123
|
-
isUnique: z.ZodOptional<z.ZodBoolean>;
|
|
124
|
-
}, z.core.$strip>, z.ZodTransform<{
|
|
42
|
+
} | FieldScalarEnriched | FieldJsonEnriched | {
|
|
125
43
|
kind: "relation";
|
|
126
44
|
type: string & z.core.$brand<"PXL.IdType"> & z.core.$brand<"PXL.Type">;
|
|
127
45
|
name: string & z.core.$brand<"PXL.FieldName">;
|
|
@@ -138,28 +56,7 @@ export declare const createFieldJSONDecoder: (params: {
|
|
|
138
56
|
isUnique: boolean;
|
|
139
57
|
validations: never[];
|
|
140
58
|
generation: string;
|
|
141
|
-
}
|
|
142
|
-
name: string;
|
|
143
|
-
type: string;
|
|
144
|
-
databaseName?: string | undefined;
|
|
145
|
-
excelName?: string | undefined;
|
|
146
|
-
prismaRelationFieldName?: string | undefined;
|
|
147
|
-
deepClone?: boolean | undefined;
|
|
148
|
-
onDelete?: "cascade" | "setNull" | "restrict" | undefined;
|
|
149
|
-
label?: string | undefined;
|
|
150
|
-
description?: string | undefined;
|
|
151
|
-
isReadonly?: boolean | undefined;
|
|
152
|
-
isUnique?: boolean | undefined;
|
|
153
|
-
}>>, z.ZodPipe<z.ZodObject<{
|
|
154
|
-
name: z.ZodString;
|
|
155
|
-
type: z.ZodPipe<z.ZodString, z.ZodTransform<string, string>>;
|
|
156
|
-
databaseName: z.ZodOptional<z.ZodString>;
|
|
157
|
-
excelName: z.ZodOptional<z.ZodString>;
|
|
158
|
-
label: z.ZodOptional<z.ZodString>;
|
|
159
|
-
description: z.ZodOptional<z.ZodString>;
|
|
160
|
-
isReadonly: z.ZodOptional<z.ZodBoolean>;
|
|
161
|
-
isUnique: z.ZodOptional<z.ZodBoolean>;
|
|
162
|
-
}, z.core.$strip>, z.ZodTransform<{
|
|
59
|
+
} | {
|
|
163
60
|
kind: "enum";
|
|
164
61
|
type: string & z.core.$brand<"PXL.EnumType"> & z.core.$brand<"PXL.Type">;
|
|
165
62
|
name: string & z.core.$brand<"PXL.FieldName">;
|
|
@@ -171,16 +68,7 @@ export declare const createFieldJSONDecoder: (params: {
|
|
|
171
68
|
isRequired: boolean;
|
|
172
69
|
isReadonly: boolean;
|
|
173
70
|
isUnique: boolean;
|
|
174
|
-
},
|
|
175
|
-
name: string;
|
|
176
|
-
type: string;
|
|
177
|
-
databaseName?: string | undefined;
|
|
178
|
-
excelName?: string | undefined;
|
|
179
|
-
label?: string | undefined;
|
|
180
|
-
description?: string | undefined;
|
|
181
|
-
isReadonly?: boolean | undefined;
|
|
182
|
-
isUnique?: boolean | undefined;
|
|
183
|
-
}>>]>;
|
|
71
|
+
}, unknown>>;
|
|
184
72
|
export type FieldEnriched = FieldIdEnriched | FieldScalarEnriched | FieldJsonEnriched | FieldRelationEnriched | FieldEnumEnriched;
|
|
185
73
|
export type FieldEnrichedNativeDatabase = FieldIdEnriched | FieldScalarEnriched | FieldJsonEnriched | FieldRelationEnriched | FieldEnumEnriched;
|
|
186
74
|
export declare const zFieldJSONInput: (params: {
|
|
@@ -209,6 +97,8 @@ export declare const zFieldJSONInput: (params: {
|
|
|
209
97
|
isCreatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
210
98
|
isUpdatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
211
99
|
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
100
|
+
min: z.ZodOptional<z.ZodNumber>;
|
|
101
|
+
max: z.ZodOptional<z.ZodNumber>;
|
|
212
102
|
faker: z.ZodOptional<z.ZodString>;
|
|
213
103
|
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
214
104
|
placeholder: z.ZodOptional<z.ZodString>;
|
package/dist/field/field.js
CHANGED
|
@@ -25,17 +25,61 @@ const scalar_1 = require("./scalar");
|
|
|
25
25
|
*/
|
|
26
26
|
/**
|
|
27
27
|
* JSON Schema for a field
|
|
28
|
+
*
|
|
29
|
+
* Rather than offering every branch to a plain `z.union`, this dispatches on `name` and
|
|
30
|
+
* `type` using the rules documented above, and reports only the errors of the branch the
|
|
31
|
+
* field actually belongs to. A union reports every branch's failure, so a scalar field
|
|
32
|
+
* carrying one bad key used to fail with `Model Int is not defined in the project!` and
|
|
33
|
+
* `Enum Int is not defined in the project!` - the last two branches complaining that a
|
|
34
|
+
* database type is not a model or an enum, while the real cause went unmentioned (#830).
|
|
35
|
+
*
|
|
36
|
+
* The dispatch order mirrors the old union order, so which branch wins for any input that
|
|
37
|
+
* decodes successfully is unchanged.
|
|
28
38
|
*/
|
|
29
|
-
const createFieldJSONDecoder = (params) =>
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
json_1.zFieldJsonJSON.transform(json_1.fieldJsonJSONTransformer),
|
|
39
|
+
const createFieldJSONDecoder = (params) => {
|
|
40
|
+
const zId = id_1.zFieldIdJSON.transform(id_1.fieldIdJSONTransformer);
|
|
41
|
+
const zScalar = scalar_1.zFieldScalarJSON.transform(scalar_1.fieldScalarJSONTransformer);
|
|
42
|
+
const zJson = json_1.zFieldJsonJSON.transform(json_1.fieldJsonJSONTransformer);
|
|
34
43
|
// As relation and enum fields have flexible type names (i.e. the names of the referenced model/enum) and we use the
|
|
35
44
|
// type property to determine the type of the field, we need to handle these last
|
|
36
|
-
(0, relation_1.createRelationJSONDecoder)(params).transform(relation_1.fieldRelationJSONTransformer)
|
|
37
|
-
(0, enum_1.createEnumJSONDecoder)(params).transform(enum_1.fieldEnumJSONTransformer)
|
|
38
|
-
|
|
45
|
+
const zRelation = (0, relation_1.createRelationJSONDecoder)(params).transform(relation_1.fieldRelationJSONTransformer);
|
|
46
|
+
const zEnum = (0, enum_1.createEnumJSONDecoder)(params).transform(enum_1.fieldEnumJSONTransformer);
|
|
47
|
+
// Relation and enum are still told apart by trying both: their `type` is a model or enum
|
|
48
|
+
// name, so neither can be recognised from the shape of the field alone. When both fail,
|
|
49
|
+
// "not defined in the project" is the accurate diagnosis rather than a misleading one.
|
|
50
|
+
const zRelationOrEnum = zod_1.z.union([zRelation, zEnum]);
|
|
51
|
+
const zAnyBranch = zod_1.z.union([zId, zScalar, zJson, zRelation, zEnum]);
|
|
52
|
+
const selectBranch = (input) => {
|
|
53
|
+
if (typeof input !== 'object' || input === null) {
|
|
54
|
+
return zAnyBranch;
|
|
55
|
+
}
|
|
56
|
+
const { name, type } = input;
|
|
57
|
+
if (name === 'id') {
|
|
58
|
+
return zId;
|
|
59
|
+
}
|
|
60
|
+
if ((0, scalar_1.isScalarDatabaseType)(type)) {
|
|
61
|
+
return zScalar;
|
|
62
|
+
}
|
|
63
|
+
if ((0, json_1.isJsonDatabaseType)(type)) {
|
|
64
|
+
return zJson;
|
|
65
|
+
}
|
|
66
|
+
if (typeof type === 'string' && type.length > 0) {
|
|
67
|
+
return zRelationOrEnum;
|
|
68
|
+
}
|
|
69
|
+
// Nothing to dispatch on - fall back to reporting every branch, as before.
|
|
70
|
+
return zAnyBranch;
|
|
71
|
+
};
|
|
72
|
+
return zod_1.z.unknown().transform((input, ctx) => {
|
|
73
|
+
const result = selectBranch(input).safeParse(input);
|
|
74
|
+
if (!result.success) {
|
|
75
|
+
for (const issue of result.error.issues) {
|
|
76
|
+
ctx.addIssue({ ...issue });
|
|
77
|
+
}
|
|
78
|
+
return zod_1.z.NEVER;
|
|
79
|
+
}
|
|
80
|
+
return result.data;
|
|
81
|
+
});
|
|
82
|
+
};
|
|
39
83
|
exports.createFieldJSONDecoder = createFieldJSONDecoder;
|
|
40
84
|
const zFieldJSONInput = (params) => zod_1.z.union([
|
|
41
85
|
//
|
package/dist/field/json.d.ts
CHANGED
|
@@ -17,6 +17,14 @@ export type FieldJson = FieldCommon<FieldKindJson> & {
|
|
|
17
17
|
*/
|
|
18
18
|
placeholder: string;
|
|
19
19
|
};
|
|
20
|
+
/**
|
|
21
|
+
* True if `type` names a JSON database type (e.g. `Json`, `JSON?`).
|
|
22
|
+
*
|
|
23
|
+
* Derived from `zDatabaseTypeJson` itself so it cannot drift from what the json
|
|
24
|
+
* decoder actually accepts. Used by the field decoder to dispatch to this branch
|
|
25
|
+
* instead of probing every branch of the field union - see `createFieldJSONDecoder`.
|
|
26
|
+
*/
|
|
27
|
+
export declare function isJsonDatabaseType(type: unknown): boolean;
|
|
20
28
|
/**
|
|
21
29
|
* JSON Schema for a json field.
|
|
22
30
|
*/
|
package/dist/field/json.js
CHANGED
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.fieldJsonTransformer = exports.zFieldJson = exports.zFieldJsonJSON = exports.fieldKindJson = void 0;
|
|
37
|
+
exports.isJsonDatabaseType = isJsonDatabaseType;
|
|
37
38
|
exports.fieldJsonJSONTransformer = fieldJsonJSONTransformer;
|
|
38
39
|
const zod_1 = require("zod");
|
|
39
40
|
const utils_1 = require("@postxl/utils");
|
|
@@ -44,6 +45,16 @@ exports.fieldKindJson = 'json';
|
|
|
44
45
|
const zDatabaseTypeJson = zod_1.z
|
|
45
46
|
.union([zod_1.z.literal('Json'), zod_1.z.literal('JSON'), zod_1.z.literal('Json?'), zod_1.z.literal('JSON?')])
|
|
46
47
|
.describe('The type of the field. This should be `Json`/`JSON` or `Json?`/`JSON?`.');
|
|
48
|
+
/**
|
|
49
|
+
* True if `type` names a JSON database type (e.g. `Json`, `JSON?`).
|
|
50
|
+
*
|
|
51
|
+
* Derived from `zDatabaseTypeJson` itself so it cannot drift from what the json
|
|
52
|
+
* decoder actually accepts. Used by the field decoder to dispatch to this branch
|
|
53
|
+
* instead of probing every branch of the field union - see `createFieldJSONDecoder`.
|
|
54
|
+
*/
|
|
55
|
+
function isJsonDatabaseType(type) {
|
|
56
|
+
return zDatabaseTypeJson.safeParse(type).success;
|
|
57
|
+
}
|
|
47
58
|
/**
|
|
48
59
|
* JSON Schema for a json field.
|
|
49
60
|
*/
|
package/dist/field/scalar.d.ts
CHANGED
|
@@ -56,6 +56,14 @@ type FieldScalarDateNoValidation = {
|
|
|
56
56
|
type: 'Date';
|
|
57
57
|
defaultValue: string | undefined;
|
|
58
58
|
};
|
|
59
|
+
/**
|
|
60
|
+
* True if `type` names a native database scalar type (e.g. `Int`, `String?`).
|
|
61
|
+
*
|
|
62
|
+
* Derived from `zDatabaseType` itself so it cannot drift from what the scalar
|
|
63
|
+
* decoder actually accepts. Used by the field decoder to dispatch to this branch
|
|
64
|
+
* instead of probing every branch of the field union - see `createFieldJSONDecoder`.
|
|
65
|
+
*/
|
|
66
|
+
export declare function isScalarDatabaseType(type: unknown): boolean;
|
|
59
67
|
/**
|
|
60
68
|
* JSON Schema for an scalar field
|
|
61
69
|
*/
|
|
@@ -72,6 +80,8 @@ export declare const zFieldScalarJSON: z.ZodObject<{
|
|
|
72
80
|
isCreatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
73
81
|
isUpdatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
74
82
|
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
83
|
+
min: z.ZodOptional<z.ZodNumber>;
|
|
84
|
+
max: z.ZodOptional<z.ZodNumber>;
|
|
75
85
|
faker: z.ZodOptional<z.ZodString>;
|
|
76
86
|
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
77
87
|
placeholder: z.ZodOptional<z.ZodString>;
|
|
@@ -97,6 +107,8 @@ export declare const zFieldScalar: z.ZodPipe<z.ZodObject<{
|
|
|
97
107
|
isCreatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
98
108
|
isUpdatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
99
109
|
maxLength: z.ZodOptional<z.ZodNumber>;
|
|
110
|
+
min: z.ZodOptional<z.ZodNumber>;
|
|
111
|
+
max: z.ZodOptional<z.ZodNumber>;
|
|
100
112
|
faker: z.ZodOptional<z.ZodString>;
|
|
101
113
|
seed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
102
114
|
placeholder: z.ZodOptional<z.ZodString>;
|
|
@@ -114,6 +126,8 @@ export declare const zFieldScalar: z.ZodPipe<z.ZodObject<{
|
|
|
114
126
|
isCreatedAt?: boolean | undefined;
|
|
115
127
|
isUpdatedAt?: boolean | undefined;
|
|
116
128
|
maxLength?: number | undefined;
|
|
129
|
+
min?: number | undefined;
|
|
130
|
+
max?: number | undefined;
|
|
117
131
|
faker?: string | undefined;
|
|
118
132
|
seed?: string | number | boolean | Date | (string | number | boolean | Date)[] | undefined;
|
|
119
133
|
placeholder?: string | undefined;
|
package/dist/field/scalar.js
CHANGED
|
@@ -34,6 +34,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.fieldScalarTransformer = exports.zFieldScalar = exports.zFieldScalarJSON = exports.fieldKindScalar = void 0;
|
|
37
|
+
exports.isScalarDatabaseType = isScalarDatabaseType;
|
|
37
38
|
exports.fieldScalarJSONTransformer = fieldScalarJSONTransformer;
|
|
38
39
|
const zod_1 = require("zod");
|
|
39
40
|
const utils_1 = require("@postxl/utils");
|
|
@@ -54,6 +55,16 @@ const zDatabaseType = zod_1.z
|
|
|
54
55
|
zod_1.z.literal('BigInt').or(zod_1.z.literal('BigInt?')),
|
|
55
56
|
])
|
|
56
57
|
.describe('The type of the field. Should be a native database type.');
|
|
58
|
+
/**
|
|
59
|
+
* True if `type` names a native database scalar type (e.g. `Int`, `String?`).
|
|
60
|
+
*
|
|
61
|
+
* Derived from `zDatabaseType` itself so it cannot drift from what the scalar
|
|
62
|
+
* decoder actually accepts. Used by the field decoder to dispatch to this branch
|
|
63
|
+
* instead of probing every branch of the field union - see `createFieldJSONDecoder`.
|
|
64
|
+
*/
|
|
65
|
+
function isScalarDatabaseType(type) {
|
|
66
|
+
return zDatabaseType.safeParse(type).success;
|
|
67
|
+
}
|
|
57
68
|
function databaseTypeToFieldTypeScalar(input, ctx) {
|
|
58
69
|
switch (input) {
|
|
59
70
|
case 'String':
|
|
@@ -102,6 +113,8 @@ exports.zFieldScalarJSON = zod_1.z
|
|
|
102
113
|
isCreatedAt: Decoders.zFieldPropertyIsCreatedAt,
|
|
103
114
|
isUpdatedAt: Decoders.zFieldPropertyIsUpdatedAt,
|
|
104
115
|
maxLength: Decoders.zFieldPropertyMaxLength,
|
|
116
|
+
min: Decoders.zFieldPropertyMin,
|
|
117
|
+
max: Decoders.zFieldPropertyMax,
|
|
105
118
|
faker: Decoders.zFieldFaker,
|
|
106
119
|
seed: Decoders.zFieldSeed,
|
|
107
120
|
placeholder: Decoders.zFieldPropertyPlaceholder,
|
|
@@ -118,6 +131,47 @@ const defaultPlaceholders = {
|
|
|
118
131
|
Bytes: () => '1234',
|
|
119
132
|
BigInt: () => '1234',
|
|
120
133
|
};
|
|
134
|
+
/**
|
|
135
|
+
* Validates `min` / `max` against the field they were declared on.
|
|
136
|
+
*
|
|
137
|
+
* Bounds only mean something on a numeric field, and on an `Int` field only as
|
|
138
|
+
* integers. A pair where `min > max` can never be satisfied by any value. Each
|
|
139
|
+
* violation is reported as an issue, which fails the decode - the generators
|
|
140
|
+
* therefore only ever see a bound that a value can actually satisfy.
|
|
141
|
+
*/
|
|
142
|
+
function decodeNumericBounds({ fieldInput, type, scalarDatabaseType, }, ctx) {
|
|
143
|
+
const { name, min, max } = fieldInput;
|
|
144
|
+
if (min === undefined && max === undefined) {
|
|
145
|
+
return { min: undefined, max: undefined };
|
|
146
|
+
}
|
|
147
|
+
if (type !== 'number') {
|
|
148
|
+
ctx.addIssue({
|
|
149
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
150
|
+
message: `min/max are only applicable for numeric fields, but field "${name}" is of type ${scalarDatabaseType}.`,
|
|
151
|
+
});
|
|
152
|
+
return { min: undefined, max: undefined };
|
|
153
|
+
}
|
|
154
|
+
if (scalarDatabaseType === 'Int' || scalarDatabaseType === 'Int?') {
|
|
155
|
+
for (const [bound, value] of [
|
|
156
|
+
['min', min],
|
|
157
|
+
['max', max],
|
|
158
|
+
]) {
|
|
159
|
+
if (value !== undefined && !Number.isInteger(value)) {
|
|
160
|
+
ctx.addIssue({
|
|
161
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
162
|
+
message: `${bound} for Int field "${name}" must be an integer, but got ${value}.`,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
if (min !== undefined && max !== undefined && min > max) {
|
|
168
|
+
ctx.addIssue({
|
|
169
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
170
|
+
message: `min (${min}) for field "${name}" must be less than or equal to max (${max}).`,
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
return { min, max };
|
|
174
|
+
}
|
|
121
175
|
function fieldScalarJSONTransformer(fieldInput, ctx) {
|
|
122
176
|
const isCreatedAt = fieldInput.isCreatedAt ?? false;
|
|
123
177
|
const isUpdatedAt = fieldInput.isUpdatedAt ?? false;
|
|
@@ -129,6 +183,7 @@ function fieldScalarJSONTransformer(fieldInput, ctx) {
|
|
|
129
183
|
const label = fieldInput.label ?? (0, utils_1.toHumanReadable)(fieldInput.name);
|
|
130
184
|
const placeholderFactory = defaultPlaceholders[scalarDatabaseType];
|
|
131
185
|
const placeholder = fieldInput.placeholder ?? placeholderFactory?.(label) ?? `${label} here...`;
|
|
186
|
+
const { min, max } = decodeNumericBounds({ fieldInput, type, scalarDatabaseType }, ctx);
|
|
132
187
|
// Validate defaultValue type matches the field type
|
|
133
188
|
const defaultValue = fieldInput.defaultValue;
|
|
134
189
|
if (defaultValue !== undefined) {
|
|
@@ -148,6 +203,22 @@ function fieldScalarJSONTransformer(fieldInput, ctx) {
|
|
|
148
203
|
message: `Default value for numeric field "${fieldInput.name}" must be a number, but got ${typeof defaultValue}.`,
|
|
149
204
|
});
|
|
150
205
|
}
|
|
206
|
+
else {
|
|
207
|
+
// A default outside the field's own bounds can never be written, so reject it
|
|
208
|
+
// here rather than let every generated decoder reject it at runtime.
|
|
209
|
+
if (min !== undefined && defaultValue < min) {
|
|
210
|
+
ctx.addIssue({
|
|
211
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
212
|
+
message: `Default value ${defaultValue} for field "${fieldInput.name}" must be at least ${min}.`,
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
if (max !== undefined && defaultValue > max) {
|
|
216
|
+
ctx.addIssue({
|
|
217
|
+
code: zod_1.z.ZodIssueCode.custom,
|
|
218
|
+
message: `Default value ${defaultValue} for field "${fieldInput.name}" must be at most ${max}.`,
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
151
222
|
break;
|
|
152
223
|
case 'boolean':
|
|
153
224
|
if (typeof defaultValue !== 'boolean') {
|
|
@@ -191,8 +262,8 @@ function fieldScalarJSONTransformer(fieldInput, ctx) {
|
|
|
191
262
|
generation: '',
|
|
192
263
|
validations: {
|
|
193
264
|
maxLength: fieldInput.maxLength,
|
|
194
|
-
min
|
|
195
|
-
max
|
|
265
|
+
min,
|
|
266
|
+
max,
|
|
196
267
|
},
|
|
197
268
|
defaultValue,
|
|
198
269
|
seed: (0, seed_1.seedTransformer)(fieldInput, ctx),
|
|
@@ -11,6 +11,8 @@ export declare const zFieldPropertyHasIndex: z.ZodOptional<z.ZodBoolean>;
|
|
|
11
11
|
export declare const zFieldPropertyIsCreatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
12
12
|
export declare const zFieldPropertyIsUpdatedAt: z.ZodOptional<z.ZodBoolean>;
|
|
13
13
|
export declare const zFieldPropertyMaxLength: z.ZodOptional<z.ZodNumber>;
|
|
14
|
+
export declare const zFieldPropertyMin: z.ZodOptional<z.ZodNumber>;
|
|
15
|
+
export declare const zFieldPropertyMax: z.ZodOptional<z.ZodNumber>;
|
|
14
16
|
export declare const zFieldFaker: z.ZodOptional<z.ZodString>;
|
|
15
17
|
export declare const zScalarValue: z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>;
|
|
16
18
|
export declare const zFieldSeed: z.ZodOptional<z.ZodUnion<[z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>, z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean, z.ZodDate]>>]>>;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.zFieldPropertyDefaultValue = exports.zFieldPropertyPlaceholder = exports.zFieldSeed = exports.zScalarValue = exports.zFieldFaker = exports.zFieldPropertyMaxLength = exports.zFieldPropertyIsUpdatedAt = exports.zFieldPropertyIsCreatedAt = exports.zFieldPropertyHasIndex = exports.zFieldPropertyIsUnique = exports.zFieldPropertyIsReadonly = exports.zFieldPropertyDescription = exports.zFieldPropertyLabel = exports.zFieldPropertyExcelName = exports.zFieldPropertyDatabaseName = exports.zFieldPropertyType = exports.zFieldPropertyName = void 0;
|
|
3
|
+
exports.zFieldPropertyDefaultValue = exports.zFieldPropertyPlaceholder = exports.zFieldSeed = exports.zScalarValue = exports.zFieldFaker = exports.zFieldPropertyMax = exports.zFieldPropertyMin = exports.zFieldPropertyMaxLength = exports.zFieldPropertyIsUpdatedAt = exports.zFieldPropertyIsCreatedAt = exports.zFieldPropertyHasIndex = exports.zFieldPropertyIsUnique = exports.zFieldPropertyIsReadonly = exports.zFieldPropertyDescription = exports.zFieldPropertyLabel = exports.zFieldPropertyExcelName = exports.zFieldPropertyDatabaseName = exports.zFieldPropertyType = exports.zFieldPropertyName = void 0;
|
|
4
4
|
const zod_1 = require("zod");
|
|
5
5
|
const utils_1 = require("@postxl/utils");
|
|
6
6
|
exports.zFieldPropertyName = zod_1.z
|
|
@@ -73,6 +73,16 @@ exports.zFieldPropertyMaxLength = zod_1.z
|
|
|
73
73
|
.int()
|
|
74
74
|
.optional()
|
|
75
75
|
.describe('If provided, the maximum length of the field. Only applicable for `String` fields.');
|
|
76
|
+
exports.zFieldPropertyMin = zod_1.z
|
|
77
|
+
//
|
|
78
|
+
.number()
|
|
79
|
+
.optional()
|
|
80
|
+
.describe('If provided, the smallest value the field accepts. Only applicable for numeric (`Int`, `Float`, `Decimal`) fields, and must be an integer on `Int` fields.');
|
|
81
|
+
exports.zFieldPropertyMax = zod_1.z
|
|
82
|
+
//
|
|
83
|
+
.number()
|
|
84
|
+
.optional()
|
|
85
|
+
.describe('If provided, the largest value the field accepts. Only applicable for numeric (`Int`, `Float`, `Decimal`) fields, and must be an integer on `Int` fields.');
|
|
76
86
|
exports.zFieldFaker = zod_1.z
|
|
77
87
|
//
|
|
78
88
|
.string()
|