Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More specific zodtypes #65

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions src/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,24 @@ const JsonSchema: z.ZodType<Json> = z.lazy(() =>

const PublicKeySchema = z.string().length(55).startsWith('B62');

interface ProofType {
name: string;
publicInput: SerializedType;
publicOutput: SerializedType;
maxProofsVerified: number;
featureFlags: Record<string, unknown>;
}

interface SerializedType {
_type?: string;
type?: 'Constant';
Comment on lines +31 to +32
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that the correct type? it's unintentional that we still use type for constants 😅 can you maybe fix that?

value?: string;
size?: number;
proof?: ProofType;
innerType?: SerializedType;
[key: string]: SerializedType | string | number | ProofType | undefined;
}

const SerializedValueSchema = z
.object({
_type: z.string(),
Expand All @@ -27,7 +45,7 @@ const SerializedValueSchema = z
})
.strict();

const ProofTypeSchema: z.ZodType<any> = z.lazy(() =>
const ProofTypeSchema: z.ZodType<ProofType> = z.lazy(() =>
z
.object({
name: z.string(),
Expand All @@ -39,7 +57,7 @@ const ProofTypeSchema: z.ZodType<any> = z.lazy(() =>
.strict()
);

const SerializedTypeSchema: z.ZodType<any> = z.lazy(() =>
const SerializedTypeSchema: z.ZodType<SerializedType> = z.lazy(() =>
z.union([
// Basic type
z
Expand Down Expand Up @@ -113,7 +131,28 @@ const SerializedSignatureSchema = z

// Node schemas

const NodeSchema: z.ZodType<any> = z.lazy(() =>
type Node =
| { type: 'owner' }
| { type: 'issuer'; credentialKey: string }
| { type: 'constant'; data: z.infer<typeof SerializedValueSchema> }
| { type: 'root' }
| { type: 'property'; key: string; inner: Node }
| { type: 'record'; data: Record<string, Node> }
| { type: 'equals'; left: Node; right: Node }
| { type: 'equalsOneOf'; input: Node; options: Node[] | Node }
| { type: 'lessThan'; left: Node; right: Node }
| { type: 'lessThanEq'; left: Node; right: Node }
| { type: 'add'; left: Node; right: Node }
| { type: 'sub'; left: Node; right: Node }
| { type: 'mul'; left: Node; right: Node }
| { type: 'div'; left: Node; right: Node }
| { type: 'and'; inputs: Node[] }
| { type: 'or'; left: Node; right: Node }
| { type: 'not'; inner: Node }
| { type: 'hash'; inputs: Node[]; prefix?: string | null }
| { type: 'ifThenElse'; condition: Node; thenNode: Node; elseNode: Node };

const NodeSchema: z.ZodType<Node> = z.lazy(() =>
z.discriminatedUnion('type', [
z
.object({
Expand Down