From eb33e6d3875c4da6e5d1cba263d9b1f8a4797faa Mon Sep 17 00:00:00 2001 From: James R Swift Date: Sat, 18 May 2024 13:01:40 +0100 Subject: [PATCH] Style workflow --- Justfile | 4 + docs/manual.typ | 507 ++++++++++++------ src/assertions-util.typ | 11 +- src/base-type.typ | 15 +- src/coercions.typ | 9 +- src/types/dictionary.typ | 9 +- src/types/logical.typ | 12 +- tests/assertions/comparative/test.typ | 36 +- tests/contexts/remove-optional-none/test.typ | 18 +- tests/contexts/strict/test.typ | 12 +- .../integration/joined-dictionaries/test.typ | 38 +- .../pubmatter/affiliations-schema/test.typ | 1 - .../pubmatter/author-schema/test.typ | 6 +- tests/logical/test.typ | 8 +- tests/types/any/test.typ | 10 +- tests/types/array/test.typ | 66 +-- tests/types/boolean/test.typ | 16 +- tests/types/choice/test.typ | 16 +- tests/types/color/test.typ | 4 +- tests/types/datetime/test.typ | 16 +- tests/types/dictionary/test.typ | 19 +- tests/types/string/test.typ | 10 +- tests/types/tuple/test.typ | 13 +- tests/utility.typ | 8 +- 24 files changed, 585 insertions(+), 279 deletions(-) diff --git a/Justfile b/Justfile index d43289a..b8a0c1c 100644 --- a/Justfile +++ b/Justfile @@ -20,3 +20,7 @@ update *args: # run ci suite ci: test doc + +# Typstyle +style: + typstyle -i format-all \ No newline at end of file diff --git a/docs/manual.typ b/docs/manual.typ index 74a6232..06fc4f8 100644 --- a/docs/manual.typ +++ b/docs/manual.typ @@ -16,7 +16,6 @@ it } - = Example usage #add-type("schema", color: rgb("#bda8ed")) @@ -25,38 +24,38 @@ #add-type("internal", color: rgb("#ff8c8c")) #example(side-by-side: true)[```typst -#let template-schema = z.dictionary(( - title: z.content(), - abstract: z.content(default: []), - dates: z.array(z.dictionary(( - type: z.content(), - date: z.string() - ))), - paper: z.schemas.papersize(default: "a4"), - authors: z.array(z.dictionary(( - name: z.string(), - corresponding: z.boolean(default: false), - orcid: z.string(optional: true) - ))), - header: z.dictionary(( - journal: z.content(default: [Journal Name]), - article-type: z.content(default: "Article"), - article-color: z.color(default: rgb(167,195,212)), - article-meta: z.content(default: []) - )), -)); - - -#z.parse( - ( - title: [This is a required title], - paper: "a3", - authors: ( (name: "Example"),) - ), - template-schema, -) - -```] + #let template-schema = z.dictionary(( + title: z.content(), + abstract: z.content(default: []), + dates: z.array(z.dictionary(( + type: z.content(), + date: z.string() + ))), + paper: z.schemas.papersize(default: "a4"), + authors: z.array(z.dictionary(( + name: z.string(), + corresponding: z.boolean(default: false), + orcid: z.string(optional: true) + ))), + header: z.dictionary(( + journal: z.content(default: [Journal Name]), + article-type: z.content(default: "Article"), + article-color: z.color(default: rgb(167,195,212)), + article-meta: z.content(default: []) + )), + )); + + + #z.parse( + ( + title: [This is a required title], + paper: "a3", + authors: ( (name: "Example"),) + ), + template-schema, + ) + + ```] = Documentation == Terminology @@ -70,19 +69,25 @@ The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL NOT", "SHOULD", "S == Use cases The interface for a template that a user expects and that the developer has implemented are rearly one and the same. Instead, the user will apply common sense and the developer will put in somewhere between a token- and a whole-hearted- attempt at making their interface intuitive. Contrary to what one might expect, this makes it more difficult for the end user to correctly guess the interface as different developers will disagree on what is and isn't intuitive, and what edge cases the developer is willing to cover. -By first providing a low-level set of tools for validating primitives upon which more complicated schemas can be defined, `Valkyrie` handles both the micro and macro of input validation. - +By first providing a low-level set of tools for validating primitives upon which more complicated schemas can be defined, `Valkyrie` handles both the micro and macro of input validation. #pagebreak() == Parsing functions -#command("parse", arg[object], arg[schemas], arg(ctx: auto), arg(scope: ("argument",)), ret: ("any","none"))[ - Validates an object against one or more schemas. *WILL* return the given object after validation if successful, or none and *MAY* throw a failed assertion error. - - #argument("object", types:"any")[ +#command( + "parse", + arg[object], + arg[schemas], + arg(ctx: auto), + arg(scope: ("argument",)), + ret: ("any", "none"), +)[ + Validates an object against one or more schemas. *WILL* return the given object after validation if successful, or none and *MAY* throw a failed assertion error. + + #argument("object", types: "any")[ Object to validate against provided schema. Object *SHOULD* statisfy the schema requirements. An error *MAY* be produced if not. ] - #argument("schemas", types:("array","schema"))[ + #argument("schemas", types: ("array", "schema"))[ Schema against which `object` is validated. Coerced into array. *MUST* be an array of valid valkyrie schema types. ] #argument("ctx", default: auto, types: "z-ctx")[ @@ -94,89 +99,225 @@ By first providing a low-level set of tools for validating primitives upon which ] - #pagebreak() == Schema definition functions For the sake of brevity and owing to their consistency, the arguments that each schema generating function accepts are listed in the table below, followed by a description of each of argument. -#let rotatex(body,angle) = style(styles => { - let size = measure(body,styles) - box(inset:(x: -size.width/2+(size.width*calc.abs(calc.cos(angle))+size.height*calc.abs(calc.sin(angle)))/2, - y: -size.height/2+(size.height*calc.abs(calc.cos(angle))+size.width*calc.abs(calc.sin(angle)))/2), - rotate(body,angle)) +#let rotatex(body, angle) = style(styles => { + let size = measure(body, styles) + box( + inset: ( + x: -size.width / 2 + ( + size.width * calc.abs(calc.cos(angle)) + size.height * calc.abs( + calc.sin(angle), + ) + ) / 2, + y: -size.height / 2 + ( + size.height * calc.abs(calc.cos(angle)) + size.width * calc.abs( + calc.sin(angle), + ) + ) / 2, + ), + rotate(body, angle), + ) }) -#align( center, table( - stroke: black + 0.75pt, - columns: (1fr,) + 12*(auto,), - inset: 9pt, - align: (horizon, horizon+center), - table.header( - [], - rotatex([*any*], -90deg), - rotatex([*array*], -90deg), - rotatex([*boolean*], -90deg), - rotatex([*color*], -90deg), - rotatex([*content*], -90deg), - rotatex([*date*], -90deg), - rotatex([*dictionary*], -90deg), - rotatex([*either*], -90deg), - rotatex([*number, integer, float*], -90deg), - rotatex([*string, ip, email*], -90deg), - rotatex([*tuple*], -90deg), - rotatex([*choice*], -90deg), +#align( + center, + table( + stroke: black + 0.75pt, + columns: (1fr,) + 12 * (auto,), + inset: 9pt, + align: (horizon, horizon + center), + table.header( + [], + rotatex([*any*], -90deg), + rotatex([*array*], -90deg), + rotatex([*boolean*], -90deg), + rotatex([*color*], -90deg), + rotatex([*content*], -90deg), + rotatex([*date*], -90deg), + rotatex([*dictionary*], -90deg), + rotatex([*either*], -90deg), + rotatex([*number, integer, float*], -90deg), + rotatex([*string, ip, email*], -90deg), + rotatex([*tuple*], -90deg), + rotatex([*choice*], -90deg), + ), + [body], + [ ], + [✔], + [ ], + [ ], + [ ], + [ ], + [✔], + [✔], + [ ], + [ ], + [✔], + [✔], + [name], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [optional], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [default], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [types], + [✔], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [✱], + [assertions], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✱], + [✱], + [✔], + [✱], + [✔], + [✱], + [pre-transform], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✱], + [✱], + [✔], + [✔], + [✔], + [✔], + [post-transform], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], + [✔], ), - [body], [ ],[✔],[ ],[ ],[ ],[ ],[✔],[✔],[ ],[ ],[✔],[✔], - [name], [✱],[✱],[✱],[✱],[✱],[✱],[✱], [✱],[✱],[✱],[✱],[✱], - [optional], [✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔], - [default], [✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔], - [types], [✔],[✱],[✱],[✱],[✱],[✱],[✱],[✱],[✱],[✱],[✱],[✱], - [assertions], [✔],[✔],[✔],[✔],[✔],[✔],[✱],[✱],[✔],[✱],[✔],[✱], - [pre-transform], [✔],[✔],[✔],[✔],[✔],[✔],[✱],[✱],[✔],[✔],[✔],[✔], - [post-transform], [✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔],[✔], -)) +) ✔ Indicates that the argument is available to the user. ✱ Indicates that while the argument is availble to the user, it may be used internally or may hold a default value. #pagebreak() -#block(breakable: false, -argument("name", default: "unknown", types: "string")[Human-friendly name of the schema for error-reporting purposes.]) - -#block(breakable: false, -argument("optional", default: false, types: "boolean")[ - Allows the value to have not been set at the time of parsing, without generating an error. - #mty.alert[If used on a dictionary, consider adding default values to child schemas instead.] - #mty.alert[If used on a array, consider relying on the default (an empty array) instead.] -]) +#block( + breakable: false, + argument( + "name", + default: "unknown", + types: "string", + )[Human-friendly name of the schema for error-reporting purposes.], +) -#block(breakable: false, -argument("default", default: none, types: "any")[ - The default value to use if object being validated is `none`. - #mty.alert[Setting a default value allows the end-user to omit it.] -]) +#block( + breakable: false, + argument("optional", default: false, types: "boolean")[ + Allows the value to have not been set at the time of parsing, without generating an error. + #mty.alert[If used on a dictionary, consider adding default values to child schemas instead.] + #mty.alert[If used on a array, consider relying on the default (an empty array) instead.] + ], +) -#block(breakable: false, -argument("types", default: (), types: "array")[Array of allowable types. If not set, all types are accepted]) +#block( + breakable: false, + argument("default", default: none, types: "any")[ + The default value to use if object being validated is `none`. + #mty.alert[Setting a default value allows the end-user to omit it.] + ], +) -#block(breakable: false, -argument("assertions", default: (), types: "array")[ - Array of assertions to be tested during object validation. see (LINK TO ASSERTIONS) +#block( + breakable: false, + argument( + "types", + default: (), + types: "array", + )[Array of allowable types. If not set, all types are accepted], +) + +#block( + breakable: false, + argument("assertions", default: (), types: "array")[ + Array of assertions to be tested during object validation. see (LINK TO ASSERTIONS) - #mty.alert[Assertions cannot modify values] -]) + #mty.alert[Assertions cannot modify values] + ], +) -#block(breakable: false, -argument("pre-transform", default: "(self,it)=>it", types: "function")[ - Transformation to apply prior to validation. Can be used to coerce values. - -]) +#block( + breakable: false, + argument("pre-transform", default: "(self,it)=>it", types: "function")[ + Transformation to apply prior to validation. Can be used to coerce values. + + ], +) -#block(breakable: false, -argument("post-transform", default: "(self,it)=>it", types: "function")[Transformation to apply after validation. Can be used to reshape values for internal use +#block( + breakable: false, + argument( + "post-transform", + default: "(self,it)=>it", + types: "function", + )[Transformation to apply after validation. Can be used to reshape values for internal use -]) + ], +) #pagebreak() #command("any", sarg[args], ret: "schema")[ @@ -184,52 +325,74 @@ argument("post-transform", default: "(self,it)=>it", types: "function")[Transfor ] #command("array", arg[schema], sarg[args], ret: "schema")[ - #argument("schema", types: "schema")[Schema against which to validate child entries. Defaults to #tidyref(none, "any").] + #argument( + "schema", + types: "schema", + )[Schema against which to validate child entries. Defaults to #tidyref(none, "any").] ] #command("boolean", sarg[args], ret: "schema")[ - Generates a schema that accepts only booleans as valid. + Generates a schema that accepts only booleans as valid. ] #command("color", sarg[args], ret: "schema")[ - Generates a schema that accepts only colors as valid. + Generates a schema that accepts only colors as valid. ] #command("content", sarg[args], ret: "schema")[ - Generates a schema that accepts only content or string as valid. + Generates a schema that accepts only content or string as valid. ] #command("date", sarg[args], ret: "schema")[ - Generates a schema that accepts only datetime objects as valid. + Generates a schema that accepts only datetime objects as valid. ] -#command("dictionary", arg(aliases: (:)), arg[schema], sarg[args], ret: "schema")[ - #argument("aliases", types: "dict", default: (:))[Dictionary representation of source to destination aliasing. Has the effect of allowing the user to key something with `source` when its `destination` that is meant.] - #argument("schema", types: "dictionary")[Dictionary of schema elements, used to define the validation rules for each entry.] +#command( + "dictionary", + arg(aliases: (:)), + arg[schema], + sarg[args], + ret: "schema", +)[ + #argument( + "aliases", + types: "dict", + default: (:), + )[Dictionary representation of source to destination aliasing. Has the effect of allowing the user to key something with `source` when its `destination` that is meant.] + #argument( + "schema", + types: "dictionary", + )[Dictionary of schema elements, used to define the validation rules for each entry.] ] -#command("either", sarg[schema], sarg[args], ret: "schema")[ - #argument("schema", types: "dictionary", is-sink: true)[Positional arguments of validation schemes in order or preference that an input value should satisfy.] +#command("either", sarg[schema], sarg[args], ret: "schema")[ + #argument( + "schema", + types: "dictionary", + is-sink: true, + )[Positional arguments of validation schemes in order or preference that an input value should satisfy.] ] #command("number", sarg[args], ret: "schema")[ - Generates a schema that accepts only numbers as valid. + Generates a schema that accepts only numbers as valid. ] #command("string", sarg[args], ret: "schema")[ - Generates a schema that accepts only strings as valid. + Generates a schema that accepts only strings as valid. ] -#command("tuple", sarg[schema], sarg[args], ret: "schema")[ - #argument("schema", types: "schema", is-sink: true)[Positional arguments of validation schemes representing a tuple.] +#command("tuple", sarg[schema], sarg[args], ret: "schema")[ + #argument( + "schema", + types: "schema", + is-sink: true, + )[Positional arguments of validation schemes representing a tuple.] ] -#command("choice", arg[choices], sarg[args], ret: "schema")[ +#command("choice", arg[choices], sarg[args], ret: "schema")[ #argument("choices", types: "array")[Array of valid inputs] ] - - #pagebreak() == Coercions #tidy-module(read("/src/coercions.typ"), name: "coerce") @@ -243,44 +406,76 @@ argument("post-transform", default: "(self,it)=>it", types: "function")[Transfor #import "@preview/fletcher:0.4.4" as fletcher: diagram, node, edge, shapes #figure( - align(center, diagram( - spacing: 2em, - node-stroke: 0.75pt, - edge-stroke: 0.75pt, - node((-2,1), [Start], corner-radius: 2pt, shape: shapes.circle), - edge("-|>"), - node((0,1), align(center)[`value` or `self.default`]), - edge("-|>"), - node((0,2), align(center)[pre-transform value], corner-radius: 2pt), - edge("-|>"), - node((0,3), align(center)[Assert type of value], corner-radius: 2pt), - - node((-1,4), align(center)[Allow #repr(none) if \ `self.optional` is #true], corner-radius: 2pt), - node((0,4), align(center)[Allow if `self.types` \ length is 0], corner-radius: 2pt), - node((1,4), align(center)[Allow `value` if type\ in `self.types`], corner-radius: 2pt), - - node((1,3), align(center)[`self.fail-validation`], corner-radius: 2pt), - edge("-|>"), - node((2,3), align(center)[throw], corner-radius: 2pt, shape: shapes.circle), - - edge((0,3), (-1,4), "-|>", bend: -20deg) , edge( (-1,4), (0,5), "-|>", bend: -20deg), - edge((0,3), (0,4), "-|>"), edge( (0,4), (0,5), "-|>"), - edge((0,3), (1,4), "-|>", bend: 20deg), edge( (1,4), (0,5), "-|>", bend: 20deg), - edge((0,3), (1,3), "-|>"), - - node((0,5), align(center)[Handle descendents \ transformation], corner-radius: 2pt), - edge("ll,uuuu", "|>--|>", align(center)[child schema \ on descendent], label-side: left, label-pos: 0.3), - edge("-|>"), - node((0,6), align(center)[Handle assertions \ transformation], corner-radius: 2pt), - edge("-|>"), - node((1,6), align(center)[post-transform `value`], corner-radius: 2pt), - edge("-|>"), - node((2,6), [end], corner-radius: 2pt, shape: shapes.circle), - ) + v(2em)), - caption: [Flow diagram representation of parsing heuristic when validating a value against a schema.] + align( + center, + diagram( + spacing: 2em, + node-stroke: 0.75pt, + edge-stroke: 0.75pt, + node((-2, 1), [Start], corner-radius: 2pt, shape: shapes.circle), + edge("-|>"), + node((0, 1), align(center)[`value` or `self.default`]), + edge("-|>"), + node((0, 2), align(center)[pre-transform value], corner-radius: 2pt), + edge("-|>"), + node((0, 3), align(center)[Assert type of value], corner-radius: 2pt), + node( + (-1, 4), + align(center)[Allow #repr(none) if \ `self.optional` is #true], + corner-radius: 2pt, + ), + node( + (0, 4), + align(center)[Allow if `self.types` \ length is 0], + corner-radius: 2pt, + ), + node( + (1, 4), + align(center)[Allow `value` if type\ in `self.types`], + corner-radius: 2pt, + ), + node((1, 3), align(center)[`self.fail-validation`], corner-radius: 2pt), + edge("-|>"), + node( + (2, 3), + align(center)[throw], + corner-radius: 2pt, + shape: shapes.circle, + ), + edge((0, 3), (-1, 4), "-|>", bend: -20deg), + edge((-1, 4), (0, 5), "-|>", bend: -20deg), + edge((0, 3), (0, 4), "-|>"), + edge((0, 4), (0, 5), "-|>"), + edge((0, 3), (1, 4), "-|>", bend: 20deg), + edge((1, 4), (0, 5), "-|>", bend: 20deg), + edge((0, 3), (1, 3), "-|>"), + node( + (0, 5), + align(center)[Handle descendents \ transformation], + corner-radius: 2pt, + ), + edge( + "ll,uuuu", + "|>--|>", + align(center)[child schema \ on descendent], + label-side: left, + label-pos: 0.3, + ), + edge("-|>"), + node( + (0, 6), + align(center)[Handle assertions \ transformation], + corner-radius: 2pt, + ), + edge("-|>"), + node((1, 6), align(center)[post-transform `value`], corner-radius: 2pt), + edge("-|>"), + node((2, 6), [end], corner-radius: 2pt, shape: shapes.circle), + ) + v(2em), + ), + caption: [Flow diagram representation of parsing heuristic when validating a value against a schema.], ) - // = Advanced Documentation // == Internal functions diff --git a/src/assertions-util.typ b/src/assertions-util.typ index 9957811..b46dd5d 100644 --- a/src/assertions-util.typ +++ b/src/assertions-util.typ @@ -31,7 +31,10 @@ #let assert-types(var, types: (), default: none, name: "") = { assert( type(var) in (type(default), ..types), - message: "" + name + " must be of type " + types.join(", ", last: " or ") + ". Got " + type(var), + message: "" + name + " must be of type " + types.join( + ", ", + last: " or ", + ) + ". Got " + type(var), ) } @@ -42,7 +45,11 @@ } #let assert-positive(var, name: "") = { - assert-soft(var, condition: var => var >= 0, message: name + " must be positive") + assert-soft( + var, + condition: var => var >= 0, + message: name + " must be positive", + ) } #let assert-positive-type(var, name: "", types: (), default: none) = { diff --git a/src/base-type.typ b/src/base-type.typ index 56127fd..5a10270 100644 --- a/src/base-type.typ +++ b/src/base-type.typ @@ -43,7 +43,10 @@ it, scope: scope, ctx: ctx, - message: "Expected " + types.join(", ", last: " or ") + ". Got " + type(it), + message: "Expected " + types.join( + ", ", + last: " or ", + ) + ". Got " + type(it), ) return false } @@ -82,7 +85,15 @@ it = (self.pre-transform)(self, it) // assert types - if (not (self.assert-type)(self, it, scope: scope, ctx: ctx, types: self.types)) { + if ( + not (self.assert-type)( + self, + it, + scope: scope, + ctx: ctx, + types: self.types, + ) + ) { return none } diff --git a/src/coercions.typ b/src/coercions.typ index b78c49e..473e838 100644 --- a/src/coercions.typ +++ b/src/coercions.typ @@ -25,7 +25,10 @@ } if (type(it) == int) { // assume this is the year - assert(it > 1000 and it < 3000, message: "The date is assumed to be a year between 1000 and 3000") + assert( + it > 1000 and it < 3000, + message: "The date is assumed to be a year between 1000 and 3000", + ) return datetime(year: it, month: 1, day: 1) } @@ -35,7 +38,9 @@ // This isn't awesome, but probably fine return datetime(year: int(it), month: 1, day: 1) } - let dateMatch = it.find(regex(`^([1|2])([0-9]{3})([-\/])([0-9]{1,2})([-\/])([0-9]{1,2})$`.text)) + let dateMatch = it.find( + regex(`^([1|2])([0-9]{3})([-\/])([0-9]{1,2})([-\/])([0-9]{1,2})$`.text), + ) if (dateMatch != none) { let parts = it.split(regex("[-\/]")) return datetime( diff --git a/src/types/dictionary.typ b/src/types/dictionary.typ index c2d7ce7..3870653 100644 --- a/src/types/dictionary.typ +++ b/src/types/dictionary.typ @@ -59,7 +59,14 @@ it.insert(key, entry) - if (entry == none and (it.at(key, default: none) != none or ctx.remove-optional-none == true)) { + if ( + entry == none and ( + it.at( + key, + default: none, + ) != none or ctx.remove-optional-none == true + ) + ) { it.remove(key, default: none) } diff --git a/src/types/logical.typ b/src/types/logical.typ index dd34c3e..f0553c0 100644 --- a/src/types/logical.typ +++ b/src/types/logical.typ @@ -7,7 +7,10 @@ /// -> schema #let either(..args) = { - assert(args.pos().len() > 0, message: "z.either requires 1 or more arguments.") + assert( + args.pos().len() > 0, + message: "z.either requires 1 or more arguments.", + ) assert-base-type-array(args.pos(), scope: ("arguments",)) base-type() + ( @@ -16,7 +19,12 @@ 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), scope: scope) + let ret = (option.validate)( + option, + it, + ctx: z-ctx(ctx, soft-error: true), + scope: scope, + ) if ret != none { return ret } diff --git a/tests/assertions/comparative/test.typ b/tests/assertions/comparative/test.typ index 95c38dc..cbe4e3c 100644 --- a/tests/assertions/comparative/test.typ +++ b/tests/assertions/comparative/test.typ @@ -10,22 +10,40 @@ #let min-number-schema(val) = z.number(assertions: (z.assert.min(val),)) -#{soft-parse(5, min-number-schema(4) ) == 5 }\ -#{soft-parse(5, min-number-schema(5) ) == 5 }\ -#{soft-parse(5, min-number-schema(6) ) == none } +#{ + soft-parse(5, min-number-schema(4)) == 5 +}\ +#{ + soft-parse(5, min-number-schema(5)) == 5 +}\ +#{ + soft-parse(5, min-number-schema(6)) == none +} == z.assert.max #let max-number-schema(val) = z.number(assertions: (z.assert.max(val),)) -#{soft-parse(5, max-number-schema(4) ) == none }\ -#{soft-parse(5, max-number-schema(5) ) == 5 }\ -#{soft-parse(5, max-number-schema(6) ) == 5 } +#{ + soft-parse(5, max-number-schema(4)) == none +}\ +#{ + soft-parse(5, max-number-schema(5)) == 5 +}\ +#{ + soft-parse(5, max-number-schema(6)) == 5 +} == z.assert.eq #let eq-number-schema(val) = z.number(assertions: (z.assert.eq(val),)) -#{soft-parse(5, eq-number-schema(4) ) == none }\ -#{soft-parse(5, eq-number-schema(5) ) == 5 }\ -#{soft-parse(5, eq-number-schema(6) ) == none } \ No newline at end of file +#{ + soft-parse(5, eq-number-schema(4)) == none +}\ +#{ + soft-parse(5, eq-number-schema(5)) == 5 +}\ +#{ + soft-parse(5, eq-number-schema(6)) == none +} \ No newline at end of file diff --git a/tests/contexts/remove-optional-none/test.typ b/tests/contexts/remove-optional-none/test.typ index 5b9ed9e..84e151c 100644 --- a/tests/contexts/remove-optional-none/test.typ +++ b/tests/contexts/remove-optional-none/test.typ @@ -2,10 +2,10 @@ #set page(height: auto, width: auto) #let test-schema = z.dictionary(( - id: z.string(optional: true), - index: z.string(optional: true), - name: z.string(), - institution: z.string(optional: true), + id: z.string(optional: true), + index: z.string(optional: true), + name: z.string(), + institution: z.string(optional: true), )) #let test-dictionary = ( @@ -13,8 +13,10 @@ id: none, ) -#z.parse(test-dictionary, test-schema , ctx: z.z-ctx(remove-optional-none: false)) - - +#z.parse( + test-dictionary, + test-schema, + ctx: z.z-ctx(remove-optional-none: false), +) -#z.parse(test-dictionary, test-schema , ctx: z.z-ctx(remove-optional-none: true)) +#z.parse(test-dictionary, test-schema, ctx: z.z-ctx(remove-optional-none: true)) diff --git a/tests/contexts/strict/test.typ b/tests/contexts/strict/test.typ index 67ad62b..733d222 100644 --- a/tests/contexts/strict/test.typ +++ b/tests/contexts/strict/test.typ @@ -12,8 +12,12 @@ ip: "1.1.251.1", ) - _ = z.parse(test-dictionary, z.dictionary(( - email: z.email(), - ip: z.ip(), - )), ctx: strict-context) + _ = z.parse( + test-dictionary, + z.dictionary(( + email: z.email(), + ip: z.ip(), + )), + ctx: strict-context, + ) } diff --git a/tests/integration/joined-dictionaries/test.typ b/tests/integration/joined-dictionaries/test.typ index 62d8f48..f3e913b 100644 --- a/tests/integration/joined-dictionaries/test.typ +++ b/tests/integration/joined-dictionaries/test.typ @@ -5,29 +5,23 @@ = Integration -#let template-schema = z.dictionary( - ( - header: z.dictionary( - ( - article-type: z.content(default: "Article"), - article-color: z.color(default: rgb(167,195,212)), - article-meta: z.content(default: []) - ) - ), - fonts: z.dictionary( - ( - header: z.string(default: "Century Gothic"), - body: z.string(default: "CMU Sans Serif") - ) - ) - ) -); +#let template-schema = z.dictionary(( + header: z.dictionary(( + article-type: z.content(default: "Article"), + article-color: z.color(default: rgb(167, 195, 212)), + article-meta: z.content(default: []), + )), + fonts: z.dictionary(( + header: z.string(default: "Century Gothic"), + body: z.string(default: "CMU Sans Serif"), + )), +)); #z.parse( ( - title:[123456], - author: "James R Swift" - ), - (z.schemas.pubmatter, template-schema), - ctx: z.z-ctx(remove-optional-none: true) + title: [123456], + author: "James R Swift", + ), + (z.schemas.pubmatter, template-schema), + ctx: z.z-ctx(remove-optional-none: true), ) \ No newline at end of file diff --git a/tests/integration/pubmatter/affiliations-schema/test.typ b/tests/integration/pubmatter/affiliations-schema/test.typ index a20751b..9e80257 100644 --- a/tests/integration/pubmatter/affiliations-schema/test.typ +++ b/tests/integration/pubmatter/affiliations-schema/test.typ @@ -7,7 +7,6 @@ == Affiliation schema - #[ #z.parse((institution: "sdf"), z.schemas.affiliation) ] diff --git a/tests/integration/pubmatter/author-schema/test.typ b/tests/integration/pubmatter/author-schema/test.typ index da1e5d3..f40f459 100644 --- a/tests/integration/pubmatter/author-schema/test.typ +++ b/tests/integration/pubmatter/author-schema/test.typ @@ -11,8 +11,8 @@ #z.parse( ( name: "James R Swift", - email: "hello@world.com" - ), - z.schemas.author + email: "hello@world.com", + ), + z.schemas.author, ) ] diff --git a/tests/logical/test.typ b/tests/logical/test.typ index 46d3457..168d900 100644 --- a/tests/logical/test.typ +++ b/tests/logical/test.typ @@ -13,7 +13,11 @@ "email": "test@hello.wor.ld", ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } \ No newline at end of file diff --git a/tests/types/any/test.typ b/tests/types/any/test.typ index d259ccb..5870cec 100644 --- a/tests/types/any/test.typ +++ b/tests/types/any/test.typ @@ -16,11 +16,15 @@ "string (empty)": "", "string (lorem)": str(lorem(20)), "dictionary (empty)": (:), - "dictionary": (foo:"bar"), + "dictionary": (foo: "bar"), ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } diff --git a/tests/types/array/test.typ b/tests/types/array/test.typ index 2a71470..a08de13 100644 --- a/tests/types/array/test.typ +++ b/tests/types/array/test.typ @@ -13,8 +13,12 @@ "array (single)": (0,), ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } @@ -23,67 +27,67 @@ #test-array #utility-expect-eq( - test: test-array, - schema: z.array(), - truth: test-array + test: test-array, + schema: z.array(), + truth: test-array, )([Test satisfies array\]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email()), - truth: test-array + test: test-array, + schema: z.array(z.email()), + truth: test-array, )([Test satisfies array\]) === Assertions - Minimum length #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.min(1),)), - truth: test-array + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.min(1),)), + truth: test-array, )([Test satisfies array\ min length 1]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.min(2),)), - truth: test-array + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.min(2),)), + truth: test-array, )([Test satisfies array\ min length 2]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.min(3),)), - truth: () + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.min(3),)), + truth: (), )([Test fails array\ min length 3]) === Assertions - Maximum length #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.max(1),)), - truth: () + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.max(1),)), + truth: (), )([Test fails array\ max length 1]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.max(2),)), - truth: test-array + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.max(2),)), + truth: test-array, )([Test satisfies array\ max length 2]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.max(3),)), + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.max(3),)), truth: test-array, )([Test satisfies array\ max length 3]) === Assertions - Exact length #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.equals(2),)), - truth: test-array + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.equals(2),)), + truth: test-array, )([Test satisfies array\ equal length 2]) #utility-expect-eq( - test: test-array, - schema: z.array(z.email(), assertions: (z.assert.length.equals(3),)), - truth: () + test: test-array, + schema: z.array(z.email(), assertions: (z.assert.length.equals(3),)), + truth: (), )([Test fails array\ equal length 3]) \ No newline at end of file diff --git a/tests/types/boolean/test.typ b/tests/types/boolean/test.typ index c880422..971f0f5 100644 --- a/tests/types/boolean/test.typ +++ b/tests/types/boolean/test.typ @@ -12,8 +12,12 @@ "boolean (false)": false, ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } @@ -23,7 +27,11 @@ "number (0)": 0, ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: none)([It should fail #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: none, + )([It should fail #name]) } } diff --git a/tests/types/choice/test.typ b/tests/types/choice/test.typ index d5b7f78..124d8b3 100644 --- a/tests/types/choice/test.typ +++ b/tests/types/choice/test.typ @@ -13,8 +13,12 @@ "choice (1)": 1, ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } @@ -24,8 +28,12 @@ "number (0)": 0, ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: none)([It should fail #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: none, + )([It should fail #name]) } } diff --git a/tests/types/color/test.typ b/tests/types/color/test.typ index efff47a..4011a97 100644 --- a/tests/types/color/test.typ +++ b/tests/types/color/test.typ @@ -2,8 +2,8 @@ #set page(height: 1cm, width: 1cm) #{ - _ = z.parse(rgb(0,0,0), z.color()) - _ = z.parse(cmyk(0%,0%,0%,0%), z.color()) + _ = z.parse(rgb(0, 0, 0), z.color()) + _ = z.parse(cmyk(0%, 0%, 0%, 0%), z.color()) //_ = z.parse(0, z.color()) //_ = z.parse(none, z.color()) } diff --git a/tests/types/datetime/test.typ b/tests/types/datetime/test.typ index 3a64696..c70ab8c 100644 --- a/tests/types/datetime/test.typ +++ b/tests/types/datetime/test.typ @@ -11,8 +11,12 @@ "date (true)": datetime.today(), ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: value)([It should validate #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: value, + )([It should validate #name]) } } @@ -22,7 +26,11 @@ "number (0)": 0, ) - for (name, value) in input-types{ - utility-expect-eq(test: value, schema: schema, truth: none)([It should fail #name]) + for (name, value) in input-types { + utility-expect-eq( + test: value, + schema: schema, + truth: none, + )([It should fail #name]) } } diff --git a/tests/types/dictionary/test.typ b/tests/types/dictionary/test.typ index 6658367..0af4efb 100644 --- a/tests/types/dictionary/test.typ +++ b/tests/types/dictionary/test.typ @@ -1,15 +1,18 @@ #import "/src/lib.typ" as z - #let test-dictionary = ( - string: "world", - number: 1.2, - email: "hello@world.com", - ip: "1.1.251.1", - ) +#let test-dictionary = ( + string: "world", + number: 1.2, + email: "hello@world.com", + ip: "1.1.251.1", +) -#z.parse(test-dictionary, z.dictionary(( +#z.parse( + test-dictionary, + z.dictionary(( string: z.string(assertions: (z.assert.length.min(5),), optional: true), number: z.number(optional: true), email: z.email(optional: true), ip: z.ip(optional: true), - ))) + )), +) diff --git a/tests/types/string/test.typ b/tests/types/string/test.typ index c2f5f80..188d226 100644 --- a/tests/types/string/test.typ +++ b/tests/types/string/test.typ @@ -4,6 +4,12 @@ #{ _ = z.parse("hello@world.co.uk", z.email()) _ = z.parse("192.168.0.1", z.ip()) - _ = z.parse("Hello world", z.string(assertions: (z.assert.length.min(5),), post-transform: (self,it)=>upper(it))) - _ = z.parse( none, z.string(default: "Hello" )) + _ = z.parse( + "Hello world", + z.string( + assertions: (z.assert.length.min(5),), + post-transform: (self, it) => upper(it), + ), + ) + _ = z.parse(none, z.string(default: "Hello")) } diff --git a/tests/types/tuple/test.typ b/tests/types/tuple/test.typ index 0ec1851..82e03d9 100644 --- a/tests/types/tuple/test.typ +++ b/tests/types/tuple/test.typ @@ -4,9 +4,12 @@ #{ let test-tuple = ("123", "email@address.co.uk", 1.1) - z.parse(test-tuple, z.tuple( - z.string(), - z.email(), - z.floating-point() - )) + z.parse( + test-tuple, + z.tuple( + z.string(), + z.email(), + z.floating-point(), + ), + ) } diff --git a/tests/utility.typ b/tests/utility.typ index e326c7a..1dd50be 100644 --- a/tests/utility.typ +++ b/tests/utility.typ @@ -8,10 +8,14 @@ #let utility-expect-eq( schema: none, test: none, - truth: none + truth: none, ) = block.with( width: 100%, inset: 8pt, radius: 4pt, - fill: if ( z.parse(test, schema, ctx: z.z-ctx(soft-error: true)) == truth) {rgb("#c4e4bd")} else {rgb("#f5d3d6")} + fill: if (z.parse(test, schema, ctx: z.z-ctx(soft-error: true)) == truth) { + rgb("#c4e4bd") + } else { + rgb("#f5d3d6") + }, ) \ No newline at end of file