Skip to content

Commit 26787c1

Browse files
committed
chore(utils): update validation
1 parent aaeda1c commit 26787c1

File tree

1 file changed

+121
-14
lines changed

1 file changed

+121
-14
lines changed

packages/utils/src/validation.ts

Lines changed: 121 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,12 @@ export const KlesiaRpcResponseSchema = z.union([
250250

251251
// TODO: Should probably move these validations to a separate file
252252

253+
interface SerializedValue {
254+
_type: string;
255+
value: Json;
256+
properties?: Record<string, unknown>;
257+
}
258+
253259
interface ProofType {
254260
name: string;
255261
publicInput: SerializedType;
@@ -258,17 +264,80 @@ interface ProofType {
258264
featureFlags: Record<string, unknown>;
259265
}
260266

261-
export interface SerializedType {
262-
_type?: string;
263-
// TODO: update based on mina-credentials
264-
type?: "Constant";
267+
interface DynamicString {
268+
_type: "DynamicString";
269+
_isFactory: true;
270+
maxLength: number;
265271
value?: string;
266-
size?: number;
267-
proof?: ProofType;
268-
innerType?: SerializedType;
269-
[key: string]: SerializedType | string | number | ProofType | undefined;
270272
}
271273

274+
interface DynamicArray {
275+
_type: "DynamicArray";
276+
_isFactory: true;
277+
maxLength: number;
278+
innerType: SerializedType;
279+
value?: SerializedValue[];
280+
}
281+
282+
interface DynamicRecord {
283+
_type: "DynamicRecord";
284+
_isFactory: true;
285+
maxEntries: number;
286+
knownShape: Record<string, SerializedType>;
287+
value?: Record<string, SerializedValue>;
288+
}
289+
290+
interface DynamicBytes {
291+
_type: "DynamicBytes";
292+
_isFactory: true;
293+
maxLength: number;
294+
value?: string; // hex string
295+
}
296+
297+
interface BasicType {
298+
_type: string;
299+
}
300+
301+
// TODO: type?
302+
interface ConstantType {
303+
type: "Constant";
304+
value: string;
305+
}
306+
307+
interface BytesType {
308+
_type: "Bytes";
309+
size: number;
310+
}
311+
312+
interface ProofTypeWrapper {
313+
_type: "Proof";
314+
proof: ProofType;
315+
}
316+
317+
interface ArrayType {
318+
_type: "Array";
319+
innerType: SerializedType;
320+
size: number;
321+
}
322+
323+
interface StructType {
324+
_type: "Struct";
325+
properties: { [key: string]: SerializedType };
326+
}
327+
328+
export type SerializedType =
329+
| BasicType
330+
| ConstantType
331+
| BytesType
332+
| ProofTypeWrapper
333+
| ArrayType
334+
| StructType
335+
| DynamicString
336+
| DynamicArray
337+
| DynamicRecord
338+
| DynamicBytes
339+
| { [key: string]: SerializedType };
340+
272341
// Private Credentials: Serialized Type and Value Schemas
273342

274343
const SerializedValueSchema = z
@@ -286,6 +355,38 @@ const SerializedDataValueSchema = z.union([
286355
z.boolean(),
287356
]);
288357

358+
// Dynamic type schemas
359+
const DynamicTypeBaseSchema = z.object({
360+
_type: z.string(),
361+
_isFactory: z.literal(true),
362+
});
363+
364+
const DynamicStringSchema = DynamicTypeBaseSchema.extend({
365+
_type: z.literal("DynamicString"),
366+
maxLength: z.number(),
367+
value: z.string().optional(),
368+
}).strict();
369+
370+
const DynamicArraySchema = DynamicTypeBaseSchema.extend({
371+
_type: z.literal("DynamicArray"),
372+
maxLength: z.number(),
373+
innerType: z.lazy(() => SerializedTypeSchema),
374+
value: z.array(SerializedValueSchema).optional(),
375+
}).strict();
376+
377+
const DynamicRecordSchema = DynamicTypeBaseSchema.extend({
378+
_type: z.literal("DynamicRecord"),
379+
maxEntries: z.number(),
380+
knownShape: z.record(z.lazy(() => SerializedTypeSchema)),
381+
value: z.record(SerializedValueSchema).optional(),
382+
}).strict();
383+
384+
const DynamicBytesSchema = DynamicTypeBaseSchema.extend({
385+
_type: z.literal("DynamicBytes"),
386+
maxLength: z.number(),
387+
value: z.string().optional(), // hex string
388+
}).strict();
389+
289390
const ProofTypeSchema: z.ZodType<ProofType> = z.lazy(() =>
290391
z
291392
.object({
@@ -335,12 +436,18 @@ const SerializedTypeSchema: z.ZodType<SerializedType> = z.lazy(() =>
335436
size: z.number(),
336437
})
337438
.strict(),
338-
z
339-
.object({
340-
_type: z.literal('Struct'),
341-
properties: z.record(SerializedTypeSchema),
342-
})
343-
.strict(),
439+
// Struct type
440+
z
441+
.object({
442+
_type: z.literal("Struct"),
443+
properties: z.record(SerializedTypeSchema),
444+
})
445+
.strict(),
446+
// Dynamic types
447+
DynamicStringSchema,
448+
DynamicArraySchema,
449+
DynamicRecordSchema,
450+
DynamicBytesSchema,
344451
// Allow records of nested types for Struct
345452
z.record(SerializedTypeSchema),
346453
]),

0 commit comments

Comments
 (0)