@postxl/schema 2.1.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.
@@ -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.ZodUnion<readonly [z.ZodPipe<z.ZodObject<{
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>;
@@ -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) => zod_1.z.union([
30
- //
31
- id_1.zFieldIdJSON.transform(id_1.fieldIdJSONTransformer),
32
- scalar_1.zFieldScalarJSON.transform(scalar_1.fieldScalarJSONTransformer),
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
  //
@@ -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
  */
@@ -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
  */
@@ -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;
@@ -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: undefined,
195
- max: undefined,
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()
@@ -53,6 +53,7 @@ export declare const zModelJSON: z.ZodPreprocess<z.ZodObject<{
53
53
  visibleFor: z.ZodArray<z.ZodString>;
54
54
  }, z.core.$strip>>;
55
55
  }, z.core.$strip>>;
56
+ exposeRestApi: z.ZodOptional<z.ZodBoolean>;
56
57
  schema: z.ZodOptional<z.ZodString>;
57
58
  databaseName: z.ZodOptional<z.ZodString>;
58
59
  excelName: z.ZodOptional<z.ZodString>;
@@ -68,6 +68,13 @@ exports.zModelJSON = zod_1.z.preprocess((input) => {
68
68
  .optional()
69
69
  .describe(`Indicates if the model is readonly. To be used for system models that should not be editable through actions (e.g. Mutation, Action).`),
70
70
  auth: project_schema_auth_1.zAuthRuleSetJSON.optional().describe('Authorization rules for this model.'),
71
+ exposeRestApi: zod_1.z
72
+ .boolean()
73
+ .optional()
74
+ .describe(`Expose this model through the generated REST API (\`backend-rest-api\`). Defaults to \`false\`.
75
+ Opt-in is deliberate: forgetting to opt out would silently publish the model's rows,
76
+ whereas forgetting to opt in only produces a missing route. The REST API as a whole is
77
+ additionally gated behind the \`REST_API_ENABLED\` environment variable.`),
71
78
  //
72
79
  schema: zod_1.z
73
80
  //
@@ -14,6 +14,7 @@ export declare function modelJSONTransformer(input: ModelJSON, ctx: z.Refinement
14
14
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
15
15
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
16
16
  isReadonly: boolean;
17
+ exposeRestApi: boolean;
17
18
  excelName: string;
18
19
  description: string;
19
20
  indexes: ({
@@ -58,6 +59,7 @@ export declare function modelJSONTransformer(input: ModelJSON, ctx: z.Refinement
58
59
  export type ModelEnriched = ReturnType<typeof modelJSONTransformer>;
59
60
  declare function addDefaults({ description, fields, standardFields, seed, faker, ...input }: ModelJSON): {
60
61
  isReadonly: boolean;
62
+ exposeRestApi: boolean;
61
63
  databaseName: string;
62
64
  excelName: string;
63
65
  description: string;
@@ -122,6 +124,7 @@ declare function brandModel({ name, schema, databaseName, standardFields, defaul
122
124
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
123
125
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
124
126
  isReadonly: boolean;
127
+ exposeRestApi: boolean;
125
128
  excelName: string;
126
129
  description: string;
127
130
  fields: {
@@ -182,6 +185,7 @@ declare function combineFields({ fields, standardFields, ...model }: BrandedMode
182
185
  keyFieldName: (string & z.core.$brand<"PXL.FieldName">) | undefined;
183
186
  defaultSort: (string & z.core.$brand<"PXL.FieldName">)[] | undefined;
184
187
  isReadonly: boolean;
188
+ exposeRestApi: boolean;
185
189
  excelName: string;
186
190
  description: string;
187
191
  indexes: ({
@@ -51,6 +51,7 @@ function addDefaults({ description, fields, standardFields, seed, faker, ...inpu
51
51
  return {
52
52
  ...input,
53
53
  isReadonly: input.isReadonly ?? false,
54
+ exposeRestApi: input.exposeRestApi ?? false,
54
55
  databaseName: input.databaseName ?? (0, utils_1.toSnakeCase)(input.name),
55
56
  excelName: input.excelName ?? (0, utils_1.toExcelColumnName)((0, utils_1.toPascalCase)((0, utils_1.pluralize)(input.name))),
56
57
  description: description ?? '',
@@ -7,6 +7,10 @@ export type Model = {
7
7
  name: Branded.ModelName;
8
8
  description: string;
9
9
  isReadonly: boolean;
10
+ /**
11
+ * Whether the model is exposed through the generated REST API. Defaults to `false`.
12
+ */
13
+ exposeRestApi: boolean;
10
14
  databaseSchema: DatabaseSchemaName;
11
15
  databaseName: string;
12
16
  excelName: string;
@@ -99,6 +99,7 @@ export declare const zProjectSchemaJSON: z.ZodPreprocess<z.ZodObject<{
99
99
  visibleFor: z.ZodArray<z.ZodString>;
100
100
  }, z.core.$strip>>;
101
101
  }, z.core.$strip>>;
102
+ exposeRestApi: z.ZodOptional<z.ZodBoolean>;
102
103
  schema: z.ZodOptional<z.ZodString>;
103
104
  databaseName: z.ZodOptional<z.ZodString>;
104
105
  excelName: z.ZodOptional<z.ZodString>;
@@ -78,6 +78,7 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
78
78
  visibleFor: z.ZodArray<z.ZodString>;
79
79
  }, z.core.$strip>>;
80
80
  }, z.core.$strip>>;
81
+ exposeRestApi: z.ZodOptional<z.ZodBoolean>;
81
82
  schema: z.ZodOptional<z.ZodString>;
82
83
  databaseName: z.ZodOptional<z.ZodString>;
83
84
  excelName: z.ZodOptional<z.ZodString>;
@@ -201,6 +202,7 @@ export declare const zProjectSchema: z.ZodPipe<z.ZodPreprocess<z.ZodObject<{
201
202
  visibleFor: string[];
202
203
  } | undefined;
203
204
  } | undefined;
205
+ exposeRestApi?: boolean | undefined;
204
206
  schema?: string | undefined;
205
207
  databaseName?: string | undefined;
206
208
  excelName?: string | undefined;
@@ -1771,6 +1771,7 @@ export declare const sampleSchemas: {
1771
1771
  faker: {
1772
1772
  items: number;
1773
1773
  };
1774
+ exposeRestApi?: never;
1774
1775
  standardFields?: never;
1775
1776
  labelField?: never;
1776
1777
  standardActions?: never;
@@ -1781,6 +1782,7 @@ export declare const sampleSchemas: {
1781
1782
  name: string;
1782
1783
  description: string;
1783
1784
  schema: string;
1785
+ exposeRestApi: boolean;
1784
1786
  fields: ({
1785
1787
  name: string;
1786
1788
  type: string;
@@ -1857,6 +1859,7 @@ export declare const sampleSchemas: {
1857
1859
  };
1858
1860
  defaultField?: never;
1859
1861
  seed?: never;
1862
+ exposeRestApi?: never;
1860
1863
  standardFields?: never;
1861
1864
  labelField?: never;
1862
1865
  isReadonly?: never;
@@ -1886,6 +1889,7 @@ export declare const sampleSchemas: {
1886
1889
  defaultField?: never;
1887
1890
  seed?: never;
1888
1891
  faker?: never;
1892
+ exposeRestApi?: never;
1889
1893
  labelField?: never;
1890
1894
  standardActions?: never;
1891
1895
  actions?: never;
@@ -69,6 +69,7 @@
69
69
  "name": "Post",
70
70
  "description": "A blog post entry.",
71
71
  "schema": "Data",
72
+ "exposeRestApi": true,
72
73
  "fields": [
73
74
  {
74
75
  "name": "title",
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@postxl/schema",
3
- "version": "2.1.0",
3
+ "version": "2.3.0",
4
4
  "description": "Decoders for PXL Schema definitions and validation for PXL code generation framework",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -36,7 +36,7 @@
36
36
  "directory": "packages/schema"
37
37
  },
38
38
  "dependencies": {
39
- "@postxl/utils": "^1.4.1"
39
+ "@postxl/utils": "^1.4.2"
40
40
  },
41
41
  "devDependencies": {},
42
42
  "wallaby": {