-
Notifications
You must be signed in to change notification settings - Fork 3
refactor: enhance resource schema validation with comprehensive Zod types #151
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a05e44
refactor: Restrict entity schema according to backend
ozsay b02d5ae
refactor: Restrict agent schema according to backend
ozsay 8d2b047
refactor: Restrict function schema according to backend
ozsay 470ef07
remove model as it's currently ignored on the server
ozsay e3586f9
mention resource path for Zod errors
ozsay db5fc20
Merge branch 'main' into entity-schema-poc
kfirstri File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,160 @@ | ||
| import { z } from "zod"; | ||
|
|
||
| export const EntitySchema = z.looseObject({ | ||
| name: z.string().min(1, "Entity name cannot be empty"), | ||
| const FieldConditionSchema = z.union([ | ||
| z.string(), | ||
| z.object({ | ||
| $in: z.unknown().optional(), | ||
| $nin: z.unknown().optional(), | ||
| $ne: z.unknown().optional(), | ||
| $all: z.unknown().optional(), | ||
| }), | ||
| ]); | ||
|
|
||
| const userConditionAllowedKeys = new Set(["role", "email", "id"]); | ||
|
|
||
| const UserConditionSchema = z | ||
| .looseObject({ | ||
| role: z.string().optional(), | ||
| email: z.string().optional(), | ||
| id: z.string().optional(), | ||
| }) | ||
| .refine( | ||
| (val) => | ||
| Object.keys(val).every( | ||
| (key) => userConditionAllowedKeys.has(key) || key.startsWith("data.") | ||
| ), | ||
| "Keys must be role, email, id, or match data.* pattern" | ||
| ) | ||
|
|
||
| const rlsConditionAllowedKeys = new Set([ | ||
| "user_condition", | ||
| "created_by", | ||
| "created_by_id", | ||
| "$or", | ||
| "$and", | ||
| "$nor", | ||
| ]); | ||
|
|
||
| const RLSConditionSchema = z | ||
| .looseObject({ | ||
| user_condition: UserConditionSchema.optional(), | ||
| created_by: FieldConditionSchema.optional(), | ||
| created_by_id: FieldConditionSchema.optional(), | ||
| get $or(): z.ZodOptional<z.ZodArray<typeof RLSConditionSchema>> { | ||
| return z.array(RefineRLSConditionSchema).optional(); | ||
| }, | ||
| get $and(): z.ZodOptional<z.ZodArray<typeof RLSConditionSchema>> { | ||
| return z.array(RefineRLSConditionSchema).optional(); | ||
| }, | ||
| get $nor(): z.ZodOptional<z.ZodArray<typeof RLSConditionSchema>> { | ||
| return z.array(RefineRLSConditionSchema).optional(); | ||
| }, | ||
| }); | ||
|
|
||
| const fieldConditionOperators = new Set(["$in", "$nin", "$ne", "$all"]); | ||
|
|
||
| const isValidFieldCondition = (value: unknown): boolean => { | ||
| // Server accepts: string, number, boolean, null, or operator object | ||
| if ( | ||
| value === null || | ||
| typeof value === "string" || | ||
| typeof value === "number" || | ||
| typeof value === "boolean" | ||
| ) { | ||
| return true; | ||
| } | ||
| if (typeof value === "object") { | ||
| return Object.keys(value).every((k) => fieldConditionOperators.has(k)); | ||
| } | ||
| return false; | ||
| }; | ||
|
|
||
| const RefineRLSConditionSchema = RLSConditionSchema.refine( | ||
| (val) => | ||
| Object.entries(val).every(([key, value]) => { | ||
| if (rlsConditionAllowedKeys.has(key)) {return true;} | ||
| if (!key.startsWith("data.")) {return false;} | ||
| return isValidFieldCondition(value); | ||
| }), | ||
| "Keys must be known RLS keys or match data.* pattern with valid value" | ||
| ); | ||
|
|
||
| const RLSRuleSchema = z.union([z.boolean(), RefineRLSConditionSchema]); | ||
|
|
||
| const EntityRLSSchema = z.strictObject({ | ||
| create: RLSRuleSchema.optional(), | ||
| read: RLSRuleSchema.optional(), | ||
| update: RLSRuleSchema.optional(), | ||
| delete: RLSRuleSchema.optional(), | ||
| write: RLSRuleSchema.optional(), | ||
| }); | ||
|
|
||
| const FieldRLSSchema = z.strictObject({ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is verified against the apper codebase? |
||
| read: RLSRuleSchema.optional(), | ||
| write: RLSRuleSchema.optional(), | ||
| create: RLSRuleSchema.optional(), | ||
| update: RLSRuleSchema.optional(), | ||
| delete: RLSRuleSchema.optional(), | ||
| }); | ||
|
|
||
| const PropertyTypeSchema = z.enum([ | ||
| "string", | ||
| "number", | ||
| "integer", | ||
| "boolean", | ||
| "array", | ||
| "object", | ||
| "binary", | ||
| ]); | ||
|
|
||
| const StringFormatSchema = z.enum([ | ||
| "date", | ||
| "date-time", | ||
| "time", | ||
| "email", | ||
| "uri", | ||
| "hostname", | ||
| "ipv4", | ||
| "ipv6", | ||
| "uuid", | ||
| "file", | ||
| "regex", | ||
| ]); | ||
|
|
||
| const PropertyDefinitionSchema = z.object({ | ||
| type: PropertyTypeSchema, | ||
| title: z.string().optional(), | ||
| description: z.string().optional(), | ||
| minLength: z.number().int().min(0).optional(), | ||
| maxLength: z.number().int().min(0).optional(), | ||
| pattern: z.string().optional(), | ||
| format: StringFormatSchema.optional(), | ||
| minimum: z.number().optional(), | ||
| maximum: z.number().optional(), | ||
| enum: z.array(z.string()).optional(), | ||
| enumNames: z.array(z.string()).optional(), | ||
| default: z.unknown().optional(), | ||
| $ref: z.string().optional(), | ||
| rls: FieldRLSSchema.optional(), | ||
| required: z.array(z.string()).optional(), | ||
| get items() { | ||
| return PropertyDefinitionSchema.optional(); | ||
| }, | ||
| get properties() { | ||
| return z.record(z.string(), PropertyDefinitionSchema).optional(); | ||
| }, | ||
| }); | ||
|
|
||
| export const EntitySchema = z.object({ | ||
| type: z.literal("object"), | ||
| name: z | ||
| .string() | ||
| .regex(/^[a-zA-Z0-9]+$/, "Entity name must be alphanumeric only"), | ||
| title: z.string().optional(), | ||
| description: z.string().optional(), | ||
| properties: z.record(z.string(), PropertyDefinitionSchema), | ||
| required: z.array(z.string()).optional(), | ||
| rls: EntityRLSSchema.optional(), | ||
| }); | ||
|
|
||
| export type Entity = z.infer<typeof EntitySchema>; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| { | ||
| "name": "Task", | ||
| "fields": [ | ||
| { "name": "title", "type": "string" }, | ||
| { "name": "description", "type": "string" } | ||
| ] | ||
| "type": "object", | ||
| "properties": { | ||
| "title": { "type": "string" }, | ||
| "description": { "type": "string" } | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets remove some of the huge commends, and also we don't need to export everything since it's not used progrematically.