The YS Core YAML-Schema
The term "YAML Schema" is a bit of unfortunate history.
It's not the YAML version of a JSON Schema.
In my opinion it needs to be renamed because it's not a schema in the typical sense of the word. The term has been part of the YAML spec since the beginning.
Let's use the term "YAML-Schema" instead of "YAML Schema" to be clear that this is not a typical schema applied to a YAML file.
Typically a schema is a set of rules that are used to validate data.
A YAML-Schema is a set of rules about how untagged nodes are implicitly tagged during the loading process.
Allow me to explain.
YAML Implicit Tagging🔗
We've covered this before, but when YAML is loaded, every node without a tag is assigned one based on "how it looks".
By default, mappings, sequences and not-unquoted-scalars are assigned the tags
!!map
, !!seq
, and !!str
respectively.
In a typical loader, every tag must have been configured to identify a constructor function. The constructor phase is the final phase of the loading process. It creates the value that the program doing the loading will see.
So what's left?
It's the plain (unquoted) scalar nodes.
In JSON, the allowed unquoted scalars are: true
, false
, null
and scalars
that look like numbers.
A YAML-Schema defines the patterns that unquoted scalars must match, and the associated tag they will be assigned.
The YS Core YAML-Schema🔗
YS tries to stick close to the YAML 1.2 spec's Core Schema
.
This is supposed to be what most YAML Loaders should use by default.
Therefore it should be the most compatible with other YAML Loaders.
I'll show the different schema tags as actual ys
commands:
# !!null
$ ys -p - <<<'"!!null": [null, Null, NULL, ~]'
{"!!null" [nil nil nil nil]}
# !!bool
$ ys -p - <<<'"!!bool": [true, True, TRUE, false, False, FALSE]'
{"!!bool" [true true true false false false]}
# !!int
$ ys -p - <<<'"!!int": [-42, -0, 0, 42, 0x123, 0o123]'
{"!!int" [-42 0 0 42 291 83]}
# !!float
$ ys -p - <<<'"!!float": [.123, 1.23, 1.2e3, -1.2e-3, 0.0, -0.0]'
{"!!float" [0.123 1.23 1200.0 -0.0012 0.0 -0.0]}
# Unsupported from YAML 1.2 Core Schema spec
$ ys -p - <<<'"!!float": [.inf, .Inf, .INF, -.inf, -.Inf, -.INF, .nan, .Nan, .NAN]'
Compile error: Inf and NaN not supported in YS
# !!str for everything else
$ ys -p - <<<'"!!str": [NulL, FaLsE, 42 + 1, 1.2.3]'
{"!!str" ["NulL" "FaLsE" "42 + 1" "1.2.3"]}
Using Other YAML-Schemas🔗
The above is the default YAML-Schema for YS.
YAML was always meant to allow you to define your own YAML-Schema.
Some implementations of YAML let you choose from a few different YAML-Schemas.
But really you should be able to trivially define your own YAML-Schema for any YAML file you want to load.
YAML never really lived up to this promise.
YS does not yet support this, but it's on the roadmap.
Stay tuned. Stay YS!