Skip to content

Commit

Permalink
Merge pull request #32 from typst-community/27-mutually-exclusive-cho…
Browse files Browse the repository at this point in the history
…ices-in-either

Changes to `either` and `dictionary`
  • Loading branch information
jamesrswift authored Jun 20, 2024
2 parents 1b307f2 + f365311 commit 3510899
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/base-type.typ
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@
}
ctx.outcome = display
assert(ctx.soft-error, message: display)
return none
},
)
}
2 changes: 1 addition & 1 deletion src/schemas/author.typ
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
),
pre-transform: coerce.dictionary(it => (name: it)),
post-transform: (self, it) => {
if (it.at("email", default: none) != none and it.corresponding == none) {
if (it.at("email", default: none) != none and "corresponding" not in it) {
it.insert("corresponding", true)
}
return it
Expand Down
29 changes: 18 additions & 11 deletions src/types/dictionary.typ
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@
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",
)
}
}
}


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

let entry = (
Expand All @@ -57,17 +73,8 @@
scope: (..scope, str(key))
)

it.insert(key, entry)

if (
entry == none and (
it.at(
key,
default: none,
) != none or ctx.remove-optional-none == true
)
) {
it.remove(key, default: none)
if (entry != none) {
it.insert(key, entry)
}

}
Expand Down
19 changes: 15 additions & 4 deletions src/types/logical.typ
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,29 @@
/// Valkyrie schema generator for objects that can be any of multiple types.
///
/// -> schema
#let either(..args) = {
#let either(
strict: false,
..args,
) = {

assert(
args.pos().len() > 0,
message: "z.either requires 1 or more arguments.",
)
assert-base-type-array(args.pos(), scope: ("arguments",))

base-type() + (
base-type(
name: "[" + args.pos().map(it => it.name).join(", ", last: " or ") + "]",
..args.named(),
) + (
strict: strict,
options: args.pos(),
handle-descendents: (self, it, ctx: z-ctx(), scope: ()) => {
for option in self.options {
let ret = (option.validate)(
option,
it,
ctx: z-ctx(ctx, soft-error: true),
ctx: z-ctx(ctx, strict: self.strict, soft-error: true),
scope: scope,
)
if ret != none {
Expand All @@ -37,7 +42,13 @@
) + ". Got " + type(it)
)

(self.fail-validation)(self, it, ctx: ctx, scope: scope, message: message)
return (self.fail-validation)(
self,
it,
ctx: ctx,
scope: scope,
message: message,
)
},
)
}
Binary file modified tests/contexts/remove-optional-none/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.
Binary file modified tests/logical/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.
20 changes: 18 additions & 2 deletions tests/logical/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@

#show: show-rule.with();

#let schema = z.either(z.email(), z.ip())

= logical/either
== Input types
#{
let schema = z.either(z.email(), z.ip())
let input-types = (
"ip (1.1.1.1)": "1.1.1.1",
"email": "[email protected]",
Expand All @@ -20,4 +19,21 @@
truth: value,
)([It should validate #name])
}
}

#{
let schema = z.either(
strict: true,
z.dictionary((
seed: z.integer(),
)),
z.dictionary((
dynamic: z.boolean(),
)),
)

z.parse(
(dynamic: false),
schema,
)
}
Binary file modified tests/schemas/author/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.
2 changes: 2 additions & 0 deletions tests/schemas/author/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
(
name: "James R Swift",
email: "hello@world.com",
// test: true,
),
z.schemas.author,
ctx: z.z-ctx(strict: true),
)
]
4 changes: 2 additions & 2 deletions tests/types/string/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@
#z.parse("192.168.0.1", z.ip())

== default
#let _ = z.parse(none, z.string(default: "Hello"))
#let _ = z.parse(none, z.string(default: "Hello"))
#let _ = z.parse(auto, z.string(default: "Hello"))
#let _ = z.parse(auto, z.string(default: "Hello", optional: true))
#let _ = repr(z.parse(auto, z.string(optional: true)))
// #z.parse(auto, z.string()) \
// #z.parse(auto, z.string()) \
#let _ = z.parse("none", z.string(default: "Hello"))
6 changes: 5 additions & 1 deletion tests/types/tuple/test.typ
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
#set page(height: 1cm, width: 1cm)

#{
let test-tuple = ("123", "[email protected]", 1.1)
let test-tuple = (
"123",
"[email protected]",
// 1.1
)

z.parse(
test-tuple,
Expand Down

0 comments on commit 3510899

Please sign in to comment.