|
| 1 | +--- |
| 2 | +summary: >- |
| 3 | + Named field-type shortcuts for inline schema |
| 4 | + frontmatter values — the registered names, the |
| 5 | + canonical CUE each one resolves to, and example |
| 6 | + usage. |
| 7 | +--- |
| 8 | +# Schema field types |
| 9 | + |
| 10 | +A **field-type shortcut** is a short name a schema |
| 11 | +frontmatter value can use instead of spelling out a CUE |
| 12 | +expression. `created: date` is shorter and harder to |
| 13 | +typo than `created: '=~"^\\d{4}-\\d{2}-\\d{2}$"'`, and |
| 14 | +every project using the shortcut gets the same regex — |
| 15 | +no three projects land on three slightly different ISO |
| 16 | +patterns. |
| 17 | + |
| 18 | +Shortcuts work the same way for inline schemas |
| 19 | +(`kinds.<name>.schema:`) and for proto.md frontmatter. |
| 20 | +The library ships embedded in the mdsmith binary; no |
| 21 | +network access is needed to resolve a shortcut. |
| 22 | + |
| 23 | +## How a value is interpreted |
| 24 | + |
| 25 | +A frontmatter value (right-hand side of `key: value` in |
| 26 | +the schema's `frontmatter:` block) is classified by |
| 27 | +shape: |
| 28 | + |
| 29 | +| Shape | Treatment | |
| 30 | +|---------------------------------------------|------------------------------------------------------------------------| |
| 31 | +| Bare identifier in the shortcut registry | Substituted with the canonical CUE expression | |
| 32 | +| Bare identifier that is a CUE built-in type | Passes through verbatim (`string`, `int`, `bool`, `float`, `bytes`, …) | |
| 33 | +| Bare identifier that is neither | **Config error** naming the field and the unknown name | |
| 34 | +| Anything else (operators, quotes, …) | Passes through verbatim as raw CUE | |
| 35 | + |
| 36 | +A "bare identifier" is a YAML scalar matching |
| 37 | +`[a-zA-Z_][a-zA-Z0-9_-]*` — letters, digits, |
| 38 | +underscores, and hyphens, starting with a letter or |
| 39 | +underscore. Anything containing whitespace, operators, |
| 40 | +quotes, brackets, or other punctuation is treated as |
| 41 | +raw CUE. |
| 42 | + |
| 43 | +The strict third row catches typos at config-load |
| 44 | +time. A schema that writes `created: iso-date` errors |
| 45 | +with `unknown shortcut "iso-date"` instead of sliding |
| 46 | +through to an undefined-reference error deep in CUE |
| 47 | +evaluation. |
| 48 | + |
| 49 | +## Registered shortcuts |
| 50 | + |
| 51 | +The following names are accepted in a schema |
| 52 | +frontmatter value: |
| 53 | + |
| 54 | +| Name | Canonical CUE | Accepts | Rejects | |
| 55 | +|------------|---------------------------------------------------------------------------|------------------------|-----------------------| |
| 56 | +| `date` | `=~"^\\d{4}-\\d{2}-\\d{2}$"` | `2024-05-01` | `2024-5-1` | |
| 57 | +| `datetime` | `=~"^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2}(Z\|[+-]\\d{2}:\\d{2})?$" ` | `2024-05-01T12:30:00Z` | `2024-05-01 12:30:00` | |
| 58 | +| `time` | `=~"^\\d{2}:\\d{2}(:\\d{2})?$"` | `12:30` | `12:3` | |
| 59 | +| `email` | `=~"^[^@\\s]+@[^@\\s]+\\.[^@\\s]+$"` | `user@example.com` | `user@@example` | |
| 60 | +| `url` | `=~"^https?://"` | `https://example.com` | `ftp://example.com` | |
| 61 | +| `filename` | `=~"^[A-Za-z0-9._-]+\\.md$"` | `notes.md` | `notes.txt` | |
| 62 | +| `nonEmpty` | `string & !=""` | `hello` | `""` | |
| 63 | + |
| 64 | +The regex bodies are shape-only. `date` accepts |
| 65 | +`2024-02-30` even though February never has 30 days; |
| 66 | +semantic validation is out of scope. |
| 67 | + |
| 68 | +## Inline schema example |
| 69 | + |
| 70 | +```yaml |
| 71 | +kinds: |
| 72 | + rfc: |
| 73 | + schema: |
| 74 | + frontmatter: |
| 75 | + created: date |
| 76 | + modified: datetime |
| 77 | + contact: email |
| 78 | + homepage: url |
| 79 | + require: |
| 80 | + filename: "RFC-[0-9][0-9][0-9][0-9].md" |
| 81 | +``` |
| 82 | +
|
| 83 | +`mdsmith check` validates every file in the `rfc` kind: |
| 84 | +`created` must match the ISO-date regex, `contact` must |
| 85 | +look like an email, and so on. |
| 86 | + |
| 87 | +## proto.md example |
| 88 | + |
| 89 | +The same shortcuts work in a proto.md schema's YAML |
| 90 | +frontmatter: |
| 91 | + |
| 92 | +```markdown |
| 93 | +--- |
| 94 | +created: date |
| 95 | +homepage: url |
| 96 | +--- |
| 97 | +# ? |
| 98 | +
|
| 99 | +## Goal |
| 100 | +
|
| 101 | +## Tasks |
| 102 | +``` |
| 103 | + |
| 104 | +Substitution happens at schema-load time. After that |
| 105 | +the validator treats the canonical CUE the same way |
| 106 | +whether a user typed it out or the loader expanded |
| 107 | +the shortcut. |
| 108 | + |
| 109 | +## Composing a shortcut with extra constraints |
| 110 | + |
| 111 | +A value that needs both a shortcut and an extra |
| 112 | +constraint is **not** a bare name. Write the canonical |
| 113 | +CUE directly: |
| 114 | + |
| 115 | +```yaml |
| 116 | +frontmatter: |
| 117 | + created: '=~"^\\d{4}-\\d{2}-\\d{2}$" & >="2020-01-01"' |
| 118 | +``` |
| 119 | + |
| 120 | +The shortcut form is only triggered when the value is |
| 121 | +exactly the bare name. Anything more complex passes |
| 122 | +through as raw CUE. |
| 123 | + |
| 124 | +## Adding a shortcut |
| 125 | + |
| 126 | +The library lives at `cue/types/types.cue` in this |
| 127 | +repository. The runtime registry is in |
| 128 | +`internal/schema/shortcuts.go`; a drift test pins the |
| 129 | +two to each other. User-defined shortcuts are not yet |
| 130 | +a configuration surface — a project that needs a |
| 131 | +custom shortcut should write the canonical CUE in a |
| 132 | +shared proto.md (or wait for a follow-up plan if the |
| 133 | +need recurs). |
| 134 | + |
| 135 | +## See also |
| 136 | + |
| 137 | +- [Schemas](../guides/schemas.md) — the broader story |
| 138 | + on inline schemas, proto.md, and per-scope rule |
| 139 | + overrides. |
| 140 | +- [MDS020](../../internal/rules/MDS020-required-structure/README.md) |
| 141 | + — the rule that surfaces schema diagnostics. |
| 142 | +- [File kinds](../guides/file-kinds.md) — how kinds |
| 143 | + attach schemas (and other rule config) to file |
| 144 | + groups. |
0 commit comments