|
| 1 | +--- |
| 2 | +title: The YS Core YAML-Schema |
| 3 | +date: 2025-07-26 |
| 4 | +draft: false |
| 5 | +authors: [ingydotnet] |
| 6 | +categories: [Summer-of-YS] |
| 7 | +edit: blog/2025-07-26.md |
| 8 | +comments: true |
| 9 | +--- |
| 10 | + |
| 11 | +The term "YAML Schema" is a bit of unfortunate history. |
| 12 | + |
| 13 | +It's not the YAML version of a JSON Schema. |
| 14 | + |
| 15 | +In my opinion it needs to be renamed because it's not a schema in the typical |
| 16 | +sense of the word. |
| 17 | +The term has been part of the YAML spec since the beginning. |
| 18 | + |
| 19 | +Let's use the term "YAML-Schema" instead of "YAML Schema" to be clear that this |
| 20 | +is not a typical schema applied to a YAML file. |
| 21 | + |
| 22 | +Typically a schema is a set of rules that are used to validate data. |
| 23 | + |
| 24 | +A YAML-Schema is a set of rules about how untagged nodes are implicitly tagged |
| 25 | +during the loading process. |
| 26 | + |
| 27 | +Allow me to explain. |
| 28 | + |
| 29 | +<!-- more --> |
| 30 | + |
| 31 | + |
| 32 | +## YAML Implicit Tagging |
| 33 | + |
| 34 | +We've covered this before, but when YAML is loaded, every node without a tag is |
| 35 | +assigned one based on "how it looks". |
| 36 | + |
| 37 | +By default, mappings, sequences and not-unquoted-scalars are assigned the tags |
| 38 | +`!!map`, `!!seq`, and `!!str` respectively. |
| 39 | + |
| 40 | +In a typical loader, every tag must have been configured to identify a |
| 41 | +constructor function. |
| 42 | +The constructor phase is the final phase of the loading process. |
| 43 | +It creates the value that the program doing the loading will see. |
| 44 | + |
| 45 | +So what's left? |
| 46 | + |
| 47 | +It's the plain (unquoted) scalar nodes. |
| 48 | + |
| 49 | +In JSON, the allowed unquoted scalars are: `true`, `false`, `null` and scalars |
| 50 | +that look like numbers. |
| 51 | + |
| 52 | +A YAML-Schema defines the patterns that unquoted scalars must match, and the |
| 53 | +associated tag they will be assigned. |
| 54 | + |
| 55 | + |
| 56 | +## The YS Core YAML-Schema |
| 57 | + |
| 58 | +YS tries to stick close to the YAML 1.2 spec's [Core Schema]( |
| 59 | +https://yaml.org/spec/1.2.2/#103-core-schema). |
| 60 | +This is supposed to be what most YAML Loaders should use by default. |
| 61 | +Therefore it should be the most compatible with other YAML Loaders. |
| 62 | + |
| 63 | +I'll show the different schema tags as actual `ys` commands: |
| 64 | + |
| 65 | +```bash |
| 66 | +# !!null |
| 67 | +$ ys -p - <<<'"!!null": [null, Null, NULL, ~]' |
| 68 | +{"!!null" [nil nil nil nil]} |
| 69 | + |
| 70 | +# !!bool |
| 71 | +$ ys -p - <<<'"!!bool": [true, True, TRUE, false, False, FALSE]' |
| 72 | +{"!!bool" [true true true false false false]} |
| 73 | + |
| 74 | +# !!int |
| 75 | +$ ys -p - <<<'"!!int": [-42, -0, 0, 42, 0x123, 0o123]' |
| 76 | +{"!!int" [-42 0 0 42 291 83]} |
| 77 | + |
| 78 | +# !!float |
| 79 | +$ ys -p - <<<'"!!float": [.123, 1.23, 1.2e3, -1.2e-3, 0.0, -0.0]' |
| 80 | +{"!!float" [0.123 1.23 1200.0 -0.0012 0.0 -0.0]} |
| 81 | + |
| 82 | +# Unsupported from YAML 1.2 Core Schema spec |
| 83 | +$ ys -p - <<<'"!!float": [.inf, .Inf, .INF, -.inf, -.Inf, -.INF, .nan, .Nan, .NAN]' |
| 84 | +Compile error: Inf and NaN not supported in YS |
| 85 | + |
| 86 | +# !!str for everything else |
| 87 | +$ ys -p - <<<'"!!str": [NulL, FaLsE, 42 + 1, 1.2.3]' |
| 88 | +{"!!str" ["NulL" "FaLsE" "42 + 1" "1.2.3"]} |
| 89 | +``` |
| 90 | + |
| 91 | + |
| 92 | +## Using Other YAML-Schemas |
| 93 | + |
| 94 | +The above is the default YAML-Schema for YS. |
| 95 | + |
| 96 | +YAML was always meant to allow you to define your own YAML-Schema. |
| 97 | + |
| 98 | +Some implementations of YAML let you choose from a few different YAML-Schemas. |
| 99 | + |
| 100 | +But really you should be able to trivially define your own YAML-Schema for any |
| 101 | +YAML file you want to load. |
| 102 | + |
| 103 | +YAML never really lived up to this promise. |
| 104 | + |
| 105 | +YS does not yet support this, but it's on the roadmap. |
| 106 | + |
| 107 | +Stay tuned. Stay YS! |
0 commit comments