-
-
Notifications
You must be signed in to change notification settings - Fork 2
Normalize repeated form choices and table layouts #1866
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
7 commits
Select commit
Hold shift + click to select a range
7584b37
Normalize declarative form inputs
stefan-burke a942e54
Consolidate declarative input handling
stefan-burke 997e33a
Address declarative input review feedback
stefan-burke 1384659
Unify declarative input sources
stefan-burke 01056a5
Address final declarative input review
stefan-burke d823c38
Merge remote-tracking branch 'origin/main' into normalize/declarative…
stefan-burke 6c5e345
Use functional column layout parsing
stefan-burke 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
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
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 |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| /** Parse and validate configurable table column layouts without loading the | ||
| * Liquid rendering engine used later to format cell values. */ | ||
|
|
||
| import * as v from "valibot"; | ||
| import { reduce } from "#fp"; | ||
| import type { Result } from "#shared/result.ts"; | ||
|
|
||
| export type ColumnLayout<TColumn extends string> = { | ||
| readonly columnKeys: readonly TColumn[]; | ||
| readonly filters: ReadonlyMap<TColumn, string>; | ||
| }; | ||
|
|
||
| const LIQUID_TAG_RE = /\{\{\s*([^}]+?)\s*\}\}/g; | ||
|
|
||
| const parseTagBody = ( | ||
| body: string, | ||
| ): { key: string; filter: string | undefined } => { | ||
| const pipeIdx = body.indexOf("|"); | ||
| if (pipeIdx === -1) return { filter: undefined, key: body.trim() }; | ||
| return { filter: body.trim(), key: body.slice(0, pipeIdx).trim() }; | ||
| }; | ||
|
|
||
| interface LayoutParts<T extends string> { | ||
| columns: T[]; | ||
| filters: Map<T, string>; | ||
| seen: Set<T>; | ||
| } | ||
|
|
||
| type CollectedLayout<T extends string> = Result<LayoutParts<T>>; | ||
|
|
||
| type ParsedLayout<T extends string> = Result< | ||
| Pick<LayoutParts<T>, "columns" | "filters"> | ||
| >; | ||
|
|
||
| type ColumnSchema = v.GenericSchema<string> & { | ||
| readonly options: readonly string[]; | ||
| }; | ||
|
|
||
| const collectColumnTag = | ||
| <TSchema extends ColumnSchema>(schema: TSchema) => | ||
| ( | ||
| result: CollectedLayout<v.InferOutput<TSchema>>, | ||
| match: RegExpMatchArray, | ||
| ): CollectedLayout<v.InferOutput<TSchema>> => { | ||
| if (!result.ok) return result; | ||
| const { key, filter } = parseTagBody(match[1]!); | ||
| const parsed = v.safeParse(schema, key); | ||
| if (!parsed.success) { | ||
| return { | ||
| error: `Unknown column "${key}". Available columns: ${schema.options.join(", ")}`, | ||
| ok: false, | ||
| }; | ||
| } | ||
| const option = parsed.output; | ||
| const { columns, filters, seen } = result.value; | ||
| if (!seen.has(option)) { | ||
| seen.add(option); | ||
| columns.push(option); | ||
| if (filter) filters.set(option, filter); | ||
| } | ||
| return result; | ||
| }; | ||
|
|
||
| const parseColumnTemplate = <TSchema extends ColumnSchema>( | ||
| template: string, | ||
| schema: TSchema, | ||
| ): ParsedLayout<v.InferOutput<TSchema>> => { | ||
| const collected = reduce(collectColumnTag(schema), { | ||
| ok: true, | ||
| value: { | ||
| columns: [], | ||
| filters: new Map(), | ||
| seen: new Set(), | ||
| }, | ||
| } satisfies CollectedLayout<v.InferOutput<TSchema>>)([ | ||
| ...template.matchAll(LIQUID_TAG_RE), | ||
| ]); | ||
| if (!collected.ok) return collected; | ||
| const { columns, filters } = collected.value; | ||
|
|
||
| if (columns.length === 0) { | ||
| return { error: "Template must include at least one column", ok: false }; | ||
| } | ||
|
|
||
| return { ok: true, value: { columns, filters } }; | ||
| }; | ||
|
|
||
| export const buildDefaultTemplate = (keys: readonly string[]): string => | ||
| keys.map((key) => `{{${key}}}`).join(", "); | ||
|
|
||
| const defineColumnLayout = < | ||
| const TDefault extends readonly [string, ...string[]], | ||
| const TExtra extends readonly string[], | ||
| >( | ||
| defaultOrder: TDefault, | ||
| extra: TExtra, | ||
| ) => { | ||
| const [first, ...rest] = defaultOrder; | ||
| const schema = v.picklist([first, ...rest, ...extra]); | ||
| const defaultLayout: ColumnLayout<TDefault[number] | TExtra[number]> = { | ||
| columnKeys: defaultOrder, | ||
| filters: new Map(), | ||
| }; | ||
| return { | ||
| defaultLayout, | ||
| defaultOrder, | ||
| defaultTemplate: buildDefaultTemplate(defaultOrder), | ||
| options: schema.options as readonly (TDefault[number] | TExtra[number])[], | ||
| parse(template: string): ColumnLayout<TDefault[number] | TExtra[number]> { | ||
| if (!template) return defaultLayout; | ||
| const result = parseColumnTemplate(template, schema); | ||
| if (!result.ok) throw new Error(result.error); | ||
| return { | ||
| columnKeys: result.value.columns, | ||
| filters: result.value.filters, | ||
| }; | ||
| }, | ||
| schema, | ||
| validate(template: string): string | null { | ||
| if (!template) return null; | ||
| const result = parseColumnTemplate(template, schema); | ||
| return result.ok ? null : result.error; | ||
| }, | ||
| }; | ||
| }; | ||
|
|
||
| export const COLUMN_LAYOUTS = { | ||
| attendee: defineColumnLayout( | ||
| [ | ||
| "status", | ||
| "date", | ||
| "name", | ||
| "listings", | ||
| "email", | ||
| "phone", | ||
| "address", | ||
| "special_instructions", | ||
| "answers", | ||
| "qty", | ||
| "ticket", | ||
| "registered", | ||
| ], | ||
| [], | ||
| ), | ||
| listing: defineColumnLayout( | ||
| [ | ||
| "name", | ||
| "description", | ||
| "status", | ||
| "attendees", | ||
| "tickets", | ||
| "revenue", | ||
| "cost", | ||
| "profit", | ||
| "created", | ||
| ], | ||
| ["date", "location", "price", "renewal"], | ||
| ), | ||
| }; | ||
|
|
||
| export type ColumnLayoutKind = keyof typeof COLUMN_LAYOUTS; | ||
| export type ListingColumn = | ||
| (typeof COLUMN_LAYOUTS)["listing"]["options"][number]; | ||
| export type AttendeeColumn = | ||
| (typeof COLUMN_LAYOUTS)["attendee"]["options"][number]; | ||
|
|
||
| export type ListingColumnLayout = ReturnType< | ||
| (typeof COLUMN_LAYOUTS)["listing"]["parse"] | ||
| >; | ||
| export type AttendeeColumnLayout = ReturnType< | ||
| (typeof COLUMN_LAYOUTS)["attendee"]["parse"] | ||
| >; |
Oops, something went wrong.
Oops, something went wrong.
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.
When an authenticated listing/modifier/answer recalculate POST contains a stale or tampered
recalculate_fieldsvalue, this exception bubbles throughrunRecalculatePost/withAuthwithout being converted to a response, so the operator gets a 500 instead of the same 400 re-render used for the empty-selection validation path. Treat the unknown choice as a form validation error at this boundary so bad request data fails closed without crashing the route.Useful? React with 👍 / 👎.