@ghentcdh/crouton-api 0.0.1-alpha.56 → 0.0.1-alpha.57
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/index.cjs +55 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +61 -5
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -2003,13 +2003,26 @@ var jsonSchemaOverride = /* @__PURE__ */ __name(({ zodSchema, jsonSchema }) => {
|
|
|
2003
2003
|
if (zodSchema instanceof import_zod21.z.ZodBigInt) {
|
|
2004
2004
|
jsonSchema.type = "integer";
|
|
2005
2005
|
}
|
|
2006
|
-
if (isInstanceOf(zodSchema, "Decimal")) {
|
|
2006
|
+
if (isInstanceOf(zodSchema, "Decimal") || isInstanceOf(zodSchema, "Decimal2")) {
|
|
2007
2007
|
jsonSchema.type = "number";
|
|
2008
2008
|
}
|
|
2009
2009
|
if (isInstanceOf(zodSchema, "Buffer")) {
|
|
2010
2010
|
jsonSchema.type = "string";
|
|
2011
2011
|
}
|
|
2012
2012
|
}, "jsonSchemaOverride");
|
|
2013
|
+
var unwrap = /* @__PURE__ */ __name((schema) => {
|
|
2014
|
+
let s = schema;
|
|
2015
|
+
let t = s._zod?.def?.type;
|
|
2016
|
+
while (t === "optional" || t === "nullable" || t === "default" || t === "readonly") {
|
|
2017
|
+
s = s._zod?.def?.innerType ?? s;
|
|
2018
|
+
t = s._zod?.def?.type;
|
|
2019
|
+
}
|
|
2020
|
+
return s;
|
|
2021
|
+
}, "unwrap");
|
|
2022
|
+
var isDecimalField = /* @__PURE__ */ __name((zodType) => {
|
|
2023
|
+
const inner = unwrap(zodType);
|
|
2024
|
+
return isInstanceOf(inner, "Decimal") || isInstanceOf(inner, "Decimal2");
|
|
2025
|
+
}, "isDecimalField");
|
|
2013
2026
|
var jsonSchemaOpts = {
|
|
2014
2027
|
unrepresentable: "any",
|
|
2015
2028
|
override: jsonSchemaOverride
|
|
@@ -5271,7 +5284,7 @@ function toJsonSchema2(schema) {
|
|
|
5271
5284
|
return schema;
|
|
5272
5285
|
}
|
|
5273
5286
|
__name(toJsonSchema2, "toJsonSchema");
|
|
5274
|
-
var
|
|
5287
|
+
var unwrap2 = /* @__PURE__ */ __name((schema) => {
|
|
5275
5288
|
let s = schema;
|
|
5276
5289
|
let t = s?._zod?.def?.type;
|
|
5277
5290
|
while (t === "optional" || t === "nullable" || t === "default" || t === "readonly") {
|
|
@@ -5290,7 +5303,7 @@ function toSelectFields(schema) {
|
|
|
5290
5303
|
}
|
|
5291
5304
|
const result = {};
|
|
5292
5305
|
for (const [key, value] of Object.entries(schema.shape)) {
|
|
5293
|
-
const inner =
|
|
5306
|
+
const inner = unwrap2(value);
|
|
5294
5307
|
const t = typeOf(inner);
|
|
5295
5308
|
if (t === "object") {
|
|
5296
5309
|
result[key] = {
|
|
@@ -5299,7 +5312,7 @@ function toSelectFields(schema) {
|
|
|
5299
5312
|
continue;
|
|
5300
5313
|
}
|
|
5301
5314
|
if (t === "array") {
|
|
5302
|
-
const element =
|
|
5315
|
+
const element = unwrap2(inner._zod.def.element);
|
|
5303
5316
|
if (typeOf(element) === "object") {
|
|
5304
5317
|
result[key] = {
|
|
5305
5318
|
select: toSelectFields(element)
|
|
@@ -6035,16 +6048,28 @@ var ZodValidationPipe = class {
|
|
|
6035
6048
|
schema;
|
|
6036
6049
|
/** Top-level field names that accept `null` (i.e. are nullable). */
|
|
6037
6050
|
nullableKeys;
|
|
6051
|
+
/** Top-level field names whose unwrapped type is a Prisma Decimal. */
|
|
6052
|
+
decimalKeys;
|
|
6053
|
+
/** Prisma Decimal constructor resolved once per pipe instance; null if unavailable. */
|
|
6054
|
+
decimalCtor;
|
|
6038
6055
|
/** Whether to coerce `undefined` → `null` on nullable fields (create/upsert only). */
|
|
6039
6056
|
coerceUndefinedToNull;
|
|
6040
6057
|
constructor(schema, options = {}) {
|
|
6041
6058
|
this.schema = schema;
|
|
6042
6059
|
this.nullableKeys = Object.entries(schema.shape).filter(([, field]) => field.safeParse(null).success).map(([key]) => key);
|
|
6060
|
+
let decimalCtor = null;
|
|
6061
|
+
try {
|
|
6062
|
+
decimalCtor = require("@prisma/client/runtime/client").Decimal;
|
|
6063
|
+
} catch {
|
|
6064
|
+
}
|
|
6065
|
+
this.decimalCtor = decimalCtor;
|
|
6066
|
+
this.decimalKeys = decimalCtor ? Object.entries(schema.shape).filter(([, field]) => isDecimalField(field)).map(([key]) => key) : [];
|
|
6043
6067
|
this.coerceUndefinedToNull = options.coerceNullableUndefinedToNull ?? false;
|
|
6044
6068
|
}
|
|
6045
6069
|
transform(value) {
|
|
6046
6070
|
const stripped = this.stripEmptyStrings(value);
|
|
6047
|
-
const
|
|
6071
|
+
const coerced = this.coerceNullable(stripped);
|
|
6072
|
+
const input = this.coerceDecimals(coerced);
|
|
6048
6073
|
const result = this.schema.safeParse(input);
|
|
6049
6074
|
if (!result.success) {
|
|
6050
6075
|
throw new CroutonValidationError(this.formatErrors(result.error));
|
|
@@ -6085,6 +6110,31 @@ var ZodValidationPipe = class {
|
|
|
6085
6110
|
}
|
|
6086
6111
|
return out;
|
|
6087
6112
|
}
|
|
6113
|
+
/**
|
|
6114
|
+
* Coerce `number` / `string` values on Decimal fields to `Prisma.Decimal`.
|
|
6115
|
+
* `null` / `undefined` / omitted keys are left untouched (PATCH-safe).
|
|
6116
|
+
* An invalid numeric string is left as-is so Zod emits a proper validation error.
|
|
6117
|
+
* Known limitation: only top-level fields are coerced (nested Decimals in relations/arrays
|
|
6118
|
+
* are out of scope — matches how nullableKeys works today).
|
|
6119
|
+
*/
|
|
6120
|
+
coerceDecimals(value) {
|
|
6121
|
+
if (!this.decimalCtor || this.decimalKeys.length === 0 || value == null || typeof value !== "object" || Array.isArray(value)) {
|
|
6122
|
+
return value;
|
|
6123
|
+
}
|
|
6124
|
+
const out = {
|
|
6125
|
+
...value
|
|
6126
|
+
};
|
|
6127
|
+
for (const key of this.decimalKeys) {
|
|
6128
|
+
const v = out[key];
|
|
6129
|
+
if (v == null) continue;
|
|
6130
|
+
if (typeof v !== "number" && typeof v !== "string") continue;
|
|
6131
|
+
try {
|
|
6132
|
+
out[key] = new this.decimalCtor(v);
|
|
6133
|
+
} catch {
|
|
6134
|
+
}
|
|
6135
|
+
}
|
|
6136
|
+
return out;
|
|
6137
|
+
}
|
|
6088
6138
|
formatErrors(error) {
|
|
6089
6139
|
return error.issues.map((e) => ({
|
|
6090
6140
|
field: e.path.join("."),
|