Crates.io | toml_schema |
lib.rs | toml_schema |
version | 0.2.1 |
source | src |
created_at | 2023-12-09 11:43:48.739056 |
updated_at | 2023-12-10 07:43:37.944521 |
description | A crate for quicky checking the structure of TOML data, similar to JSON schemas |
homepage | |
repository | https://github.com/breakmit-0/toml_schema |
max_upload_size | |
id | 1063646 |
size | 49,761 |
This crates aims to provide something similar to JSON schemas for TOML
This crate is very much new and a lot of functionnalities are not fully tested
A schema is represented by a table with a type
key, its value may be any of
string
: any string that matches the specified regexint
: a 64 bit signed integer with optional boundsfloat
: a 64 bit float with optional boundsbool
: a booleandate
: a datearray
: an array of values that all match a specific schematable
: a TOML table with specific keysalternative
: an OR operation on sub-patternsIf the parser expects a schema but finds not type
key, it will assume the type is table
, but if the type is none of
the above, parsing will fail
For each type of schema there, are other keys that are either required of optional to give more details about the schema
A default
key may also be provided when the schema is the value of a key in a table
schema
to make that key optional, default
will be ignored in other positions
Any extra keys will be ignored (except in table
regex
(optional, default = /.*/
) : a regular expression that must be found in the string, if you want to match the whole string,
use '^' and '$'min
(optional, default = [i64::MIN]) : the minmimum value allowedmax
(optional, default = [i64::MAX]) : the maximum value allowedmin
(optional, default = [f64::NEG_INFINITY]) : the minmimum value allowedmax
(optional, default = [f64::INFINITY]) : the maximum value allowednan_ok
(optional, default = false
) : if this is true, [f64::NAN] is acceptedchild
(required) : a schema that all elements of this array must matchmin
(optional, default = 0
) : the minimum number of elementsmax
(optional, default = [usize::MAX]) : the maximum number of elementsextras
(optional, default = []
) : an array of tables with a key
and schema
key that defines regex-based key-value pairsextras[n].key
(required) : a regular expression that must be found in the keyextras[n].schema
(required) : a schema that must be matched by the valuemin
(optional, default = 0
) : the minimum number of extra keysmax
(optional, default = 0
) : the maximum number of extra keysAll other keys must be schemas, they defined a table key (optional if default
is provided in this schema) that must match
the schema, a $
is stripped from the beginning of the key if it exists to allow escaping schema keywords, if you want a key
that starts with $
, start your key with $$
etc...
All keys in the TOML table beeing matched are matched against entries before extra keys, this means that if a key matches an entry and an extra, it will not count towards the number of extra keys, this means that you may want to make extra key regular expressions mutually excusive with the table entries
options
(required) : an array of schemas, a TOML value matches if any of them matchtype = "table"
extras = [{key = ".*", schema = {type = "anything"}}]
type = "array"
child = {type = "string"}
you may find a basic schema for Cargo.toml files on github at "test_files/test_schema.toml"
reference
: a link to another schema (or the schema itself)