Skip to content

Commit

Permalink
use valkyrie:0.2.1 to parse validate input
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesrswift committed Jul 18, 2024
1 parent 323b20c commit 7d83c65
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/deps.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#import "@preview/valkyrie:0.2.1" as z
13 changes: 11 additions & 2 deletions src/impl.typ
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#import "styles.typ": *
#import "note.typ"
#import "deps.typ": *
#import "validation.typ"

// Query is an array of key, value, and inherit
#let recurse-columns(columns, queries) = {
Expand Down Expand Up @@ -140,6 +142,9 @@
..args
) = {

// Recurse once to handle input validate
columns = z.parse(columns, validation.columns-schema)

let (columns, max-depth, length) = sanitize-input(columns, depth: 0)
let query-result = recurse-columns(columns, (
fill: (inherit: true, default: none),
Expand All @@ -160,8 +165,12 @@
stroke: none,
fill: query-result.map(it=>it.fill.value),
align: query-result.map(it=>it.align.value),
column-gutter: query-result.map(it=>it.gutter.value),
columns: query-result.map(it=>it.width.value),
column-gutter: query-result.map(
it=>{if it.gutter.value != none {it.gutter.value} else {auto}}
),
columns: query-result.map(
it=>{if it.width.value != none {it.width.value} else {auto}}
),
table.header(
table.hline(stroke: toprule),
..build-header(columns, max-depth: max-depth),
Expand Down
64 changes: 64 additions & 0 deletions src/validation.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#import "deps.typ": *

#let columns-schema = z.array(
z.dictionary((:
key: z.any(),
header: z.content(optional: true),
display: z.function(optional: true),
fill: z.color(optional: true),
align: z.alignment(default: start),
gutter: z.length(optional: true),
width: z.any(optional: true),
)) + (:
handle-descendents: (self, it, ctx: z.z-ctx(), scope: ()) => {

if (it.len() == 0 and self.optional) {
return none
}

// Check `it` if strict
if (ctx.strict == true) {
for (key, value) in it {
if (key not in self.dictionary-schema) {
return (self.fail-validation)(
self,
it,
ctx: ctx,
scope: scope,
message: "Unknown key `" + key + "` in dictionary",
)
}
}
}

if "children" in it {
it.children = z.parse(
it.children,
z.array(
self,
z.coerce.array
),
ctx: ctx
)
}

for (key, schema) in self.dictionary-schema {

let entry = (
schema.validate
)(
schema,
it.at(key, default: none), // implicitly handles missing entries
ctx: ctx,
scope: (..scope, str(key))
)

if (entry != none or ctx.remove-optional-none == false) {
it.insert(key, entry)
}

}
return it
},
)
)
Binary file removed tests/test1/ref/1.png
Binary file not shown.
1 change: 0 additions & 1 deletion tests/test1/test.typ

This file was deleted.

Binary file added tests/topdown/ledger/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
87 changes: 87 additions & 0 deletions tests/topdown/ledger/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#import "/src/lib.typ" as dining-table

#let data = (
(
date: datetime.today(),
particulars: lorem(05),
ledger: [JRS123] + dining-table.note.make[Hello World],
amount: (unit: $100$, decimal: $00$),
total: (unit: $99$, decimal: $00$),
),
)*7

#import "@preview/typpuccino:0.1.0"
#let bg-fill-1 = typpuccino.latte.base
#let bg-fill-2 = typpuccino.latte.mantle

#let example = (
(
key: "date",
header: align(left)[Date],
display: (it)=>it.display(auto),
fill: bg-fill-1,
align: start,
gutter: 0.5em,
),
(
key: "particulars",
header: text(tracking: 5pt)[Particulars],
width: 1fr,
gutter: 0.5em,
),
(
key: "ledger",
header: [Ledger],
fill: bg-fill-2,
width: 2cm,
gutter: 0.5em,
),
(
header: align(center)[Amount],
fill: bg-fill-1,
gutter: 0.5em,
hline: arguments(stroke: dining-table.lightrule),
children: (
(
key: "amount.unit",
header: align(left)[£],
width: 5em,
align: right,
vline: arguments(stroke: dining-table.lightrule),
gutter: 0em,
),
(
key: "amount.decimal",
header: align(right, text(number-type: "old-style")[.00]),
align: left
),
)
),
(
header: align(center)[Total],
gutter: 0.5em,
hline: arguments(stroke: dining-table.lightrule),
children: (
(
key: "total.unit",
header: align(left)[£],
width: 5em,
align: right,
vline: arguments(stroke: dining-table.lightrule),
gutter: 0em,
),
(
key: "total.decimal",
header: align(right, text(number-type: "old-style")[.00]),
align: left
),
)
),
)

#set text(size: 11pt)
#set page(height: auto, margin: 1em)
#dining-table.make(columns: example,
data: data,
notes: dining-table.note.display-list
)
Binary file added tests/validation/test-1/ref/1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
78 changes: 78 additions & 0 deletions tests/validation/test-1/test.typ
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#import "/src/deps.typ": *
#import "/src/validation.typ"
#import "/src/lib.typ" as dining-table

#import "@preview/typpuccino:0.1.0"
#let bg-fill-1 = typpuccino.latte.base
#let bg-fill-2 = typpuccino.latte.mantle

#let example = (
(
key: "date",
header: align(left)[Date],
display: (it)=>it.display(auto),
fill: bg-fill-1,
align: start,
gutter: 0.5em,
),
(
key: "particulars",
header: text(tracking: 5pt)[Particulars],
width: 1fr,
gutter: 0.5em,
),
(
key: "ledger",
header: [Ledger],
fill: bg-fill-2,
width: 2cm,
gutter: 0.5em,
),
(
header: align(center)[Amount],
fill: bg-fill-1,
gutter: 0.5em,
hline: arguments(stroke: dining-table.lightrule),
children: (
(
key: "amount.unit",
header: align(left)[£],
width: 5em,
align: right,
vline: arguments(stroke: dining-table.lightrule),
gutter: 0em,
),
(
key: "amount.decimal",
header: align(right, text(number-type: "old-style")[.00]),
align: left
),
)
),
(
header: align(center)[Total],
gutter: 0.5em,
hline: arguments(stroke: dining-table.lightrule),
children: (
(
key: "total.unit",
header: align(left)[£],
width: 5em,
align: right,
vline: arguments(stroke: dining-table.lightrule),
gutter: 0em,
),
(
key: "total.decimal",
header: align(right, text(number-type: "old-style")[.00]),
align: left
),
)
),
)

#set text(size: 11pt)
#set page(height: auto, margin: 1em)


#z.parse(example, validation.columns-schema, ctx: z.z-ctx(remove-optional-none: true))

0 comments on commit 7d83c65

Please sign in to comment.