You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Determines if a scalar type is a date/time type.
113
+
*/
114
+
functionisDateTimeType(type: Scalar): boolean{
115
+
constdateTimeTypes=[
116
+
"utcDateTime",
117
+
"offsetDateTime",
118
+
"plainDate",
119
+
"plainTime",
120
+
];
121
+
122
+
// Check the type itself first
123
+
if(dateTimeTypes.includes(type.name)){
124
+
returntrue;
125
+
}
126
+
127
+
// Walk up the base scalar chain
128
+
letbaseType=type.baseScalar;
129
+
while(baseType){
130
+
if(dateTimeTypes.includes(baseType.name)){
131
+
returntrue;
132
+
}
133
+
baseType=baseType.baseScalar;
134
+
}
135
+
136
+
returnfalse;
137
+
}
138
+
139
+
/**
140
+
* Gets the base scalar name for a type (for datetime type identification).
141
+
*/
142
+
functiongetBaseScalarName(type: Scalar): string{
143
+
constdateTimeTypes=[
144
+
"utcDateTime",
145
+
"offsetDateTime",
146
+
"plainDate",
147
+
"plainTime",
148
+
];
149
+
150
+
// Check the type itself first
151
+
if(dateTimeTypes.includes(type.name)){
152
+
returntype.name;
153
+
}
154
+
155
+
// Walk up the base scalar chain to find a datetime type
156
+
letbaseType=type.baseScalar;
157
+
while(baseType){
158
+
if(dateTimeTypes.includes(baseType.name)){
159
+
returnbaseType.name;
160
+
}
161
+
baseType=baseType.baseScalar;
162
+
}
163
+
164
+
returntype.name;
165
+
}
166
+
167
+
interfaceValidationConstraints{
168
+
minLength?: number;
169
+
maxLength?: number;
170
+
minValue?: number;
171
+
maxValue?: number;
172
+
pattern?: string;
173
+
format?: string;
174
+
isInteger?: boolean;
175
+
isFloat?: boolean;
176
+
isDateTime?: boolean;
177
+
dateTimeType?: string;
178
+
enumValues?: string[];
179
+
}
180
+
181
+
/**
182
+
* Builds a validation function for ElectroDB based on constraints.
183
+
*/
184
+
functionbuildValidationFunction(
185
+
constraints: ValidationConstraints,
186
+
): ((value: unknown)=>void)|undefined{
187
+
constchecks: string[]=[];
188
+
189
+
// String length validation
190
+
if(constraints.minLength!==undefined){
191
+
checks.push(
192
+
`if (typeof value === "string" && value.length < ${constraints.minLength}) throw new Error("Value must be at least ${constraints.minLength} characters")`,
193
+
);
194
+
}
195
+
if(constraints.maxLength!==undefined){
196
+
checks.push(
197
+
`if (typeof value === "string" && value.length > ${constraints.maxLength}) throw new Error("Value must be at most ${constraints.maxLength} characters")`,
198
+
);
199
+
}
200
+
201
+
// Numeric validation
202
+
if(constraints.minValue!==undefined){
203
+
checks.push(
204
+
`if (typeof value === "number" && value < ${constraints.minValue}) throw new Error("Value must be at least ${constraints.minValue}")`,
205
+
);
206
+
}
207
+
if(constraints.maxValue!==undefined){
208
+
checks.push(
209
+
`if (typeof value === "number" && value > ${constraints.maxValue}) throw new Error("Value must be at most ${constraints.maxValue}")`,
210
+
);
211
+
}
212
+
213
+
// Integer validation
214
+
if(constraints.isInteger){
215
+
checks.push(
216
+
`if (typeof value === "number" && !Number.isInteger(value)) throw new Error("Value must be an integer")`,
217
+
);
218
+
}
219
+
220
+
// Float validation (ensure it's a finite number)
221
+
if(constraints.isFloat){
222
+
checks.push(
223
+
`if (typeof value === "number" && !Number.isFinite(value)) throw new Error("Value must be a finite number")`,
`if (typeof value === "string") { const d = new Date(value); if (isNaN(d.getTime())) throw new Error("Value must be a valid UTC date-time string"); }`,
241
+
);
242
+
break;
243
+
case"offsetDateTime":
244
+
checks.push(
245
+
`if (typeof value === "string") { const d = new Date(value); if (isNaN(d.getTime())) throw new Error("Value must be a valid offset date-time string"); }`,
246
+
);
247
+
break;
248
+
case"plainDate":
249
+
checks.push(
250
+
`if (typeof value === "string" && !/^\\d{4}-\\d{2}-\\d{2}$/.test(value)) throw new Error("Value must be a valid date (YYYY-MM-DD)")`,
251
+
);
252
+
break;
253
+
case"plainTime":
254
+
checks.push(
255
+
`if (typeof value === "string" && !/^\\d{2}:\\d{2}(:\\d{2})?(\\.\\d+)?$/.test(value)) throw new Error("Value must be a valid time (HH:MM:SS)")`,
0 commit comments