Skip to content

Commit 0cd01e9

Browse files
committed
docs: update for v5
1 parent c5a61a4 commit 0cd01e9

4 files changed

Lines changed: 332 additions & 69 deletions

File tree

CHANGELOG.md

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,60 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88

9+
## [Unreleased]
10+
### Added
11+
- Added named exports for schemas, tags, parser events and AST utilities.
12+
- Reworked `JSON_SCHEMA` and `CORE_SCHEMA` with spec-compliant scalar resolution
13+
rules, and added `YAML11_SCHEMA`.
14+
- Added `realMapTag` for lossless mappings with non-string and complex keys.
15+
Object-based mappings now reject complex keys instead of stringifying them.
16+
- Added `dump()` `transform` option for changing the generated AST before
17+
rendering.
18+
- Added `dump()` options `seqInlineFirst`, `flowBracketPadding`,
19+
`flowSkipCommaSpace`, `flowSkipColonSpace`, `quoteFlowKeys`, `quoteStyle` and
20+
`tagBeforeAnchor`.
21+
- Added formal data layers (events and AST) for modular data pipelines.
22+
- Added low-level parser (to events), presenter and visitor APIs.
23+
- Added the [YAML Test Suite](https://github.com/yaml/yaml-test-suite) to the
24+
test set.
25+
26+
### Changed
27+
- See the [migration guide](docs/migrate_v4_to_v5.md) for upgrade notes.
28+
- Rewritten in TypeScript and reorganized the public API around flat named
29+
exports.
30+
- Reduced the set of exported schemas:
31+
- YAML 1.2 schemas: `CORE_SCHEMA` (loader default), `JSON_SCHEMA`,
32+
`FAILSAFE_SCHEMA`.
33+
- `YAML11_SCHEMA`, a combination of all YAML 1.1 tags (YAML 1.1 does not
34+
specify a schema, only "types").
35+
- `load`/`dump` default behaviour is now specified exactly via schemas:
36+
- `load` uses `CORE_SCHEMA`, without `!!merge` by default.
37+
- `dump` uses `YAML11_SCHEMA` + `CORE_SCHEMA` for the quoting check, to
38+
guarantee backward compatibility by default.
39+
- `!!set` is now loaded as a JavaScript `Set`.
40+
- Replaced the `Type` API with a tags API. Similar, but more precise and
41+
simpler. See examples for details. Tags can be defined via
42+
`defineScalarTag()`, `defineSequenceTag()` and `defineMappingTag()`, or as a
43+
spread + override of an existing tag.
44+
- Renamed `Schema.extend()` to `Schema.withTags()`.
45+
- Expanded YAML 1.2 conformance and improved handling of directives, document
46+
markers, block keys, multiline scalars, tag syntax and other things.
47+
- `load()` now throws on empty input instead of returning `undefined`.
48+
- Moved browser builds to the `js-yaml/browser` export.
49+
- Deprecated the `loadAll` signature with an iterator (still works, but is a
50+
candidate for removal).
51+
52+
### Removed
53+
- Removed deprecated `safeLoad()`, `safeLoadAll()` and `safeDump()` exports.
54+
- Removed `DEFAULT_SCHEMA` and the nested `types` export.
55+
- Removed loader options `onWarning`, `legacy` and `listener`.
56+
- Removed dumper options `styles`, `replacer`, `noCompatMode`, `condenseFlow`,
57+
`quotingType` and `forceQuotes`. Renamed `noArrayIndent` to `seqNoIndent`.
58+
Formatting and representation are now configured through presenter options,
59+
schemas and tag definitions. See migration guide on how to replace.
60+
- Removed support for importing internal files from `lib/`.
61+
62+
963
## [4.2.0] - 2026-06-01
1064
### Added
1165
- Added `docs/safety.md` with notes about processing untrusted YAML.
@@ -77,7 +131,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77131
- Code snippet created in exceptions now contains multiple lines with line numbers.
78132
- `dump()` now serializes `undefined` as `null` in collections and removes keys with
79133
`undefined` in mappings, #571.
80-
- `dump()` with `skipInvalid=true` now serializes invalid items in collections as null.
81134
- Custom tags starting with `!` are now dumped as `!tag` instead of `!<!tag>`, #576.
82135
- Custom tags starting with `tag:yaml.org,2002:` are now shorthanded using `!!`, #258.
83136

README.md

Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ JS-YAML - YAML 1.2 parser / writer for JavaScript
77
__[Online Demo](https://nodeca.github.io/js-yaml/)__
88

99

10-
This is an implementation of [YAML](https://yaml.org/), a human-friendly data
11-
serialization language. Started as [PyYAML](https://pyyaml.org/) port, it was
12-
completely rewritten from scratch. Now it's very fast, and supports 1.2 spec.
10+
A fast and complete [YAML](https://yaml.org/) parser and writer for JavaScript.
11+
Supports both the 1.2 and 1.1 specs, and passes the entire
12+
[YAML Test Suite](https://github.com/yaml/yaml-test-suite).
1313

1414

1515
Installation
@@ -19,13 +19,14 @@ Installation
1919
npm install js-yaml
2020
```
2121

22+
Upgrading from v4? See the [v5 migration guide](docs/migrate_v4_to_v5.md).
23+
2224

2325
API
2426
---
2527

26-
Here we cover the most 'useful' methods. If you need advanced details (creating
27-
your own tags), see [examples](https://github.com/nodeca/js-yaml/tree/master/examples)
28-
for more info.
28+
Here we cover the most useful methods. If you need advanced details (such as
29+
creating your own tags), see the [examples](examples/) for more info.
2930

3031
``` javascript
3132
import { load } from 'js-yaml'
@@ -43,38 +44,57 @@ try {
4344

4445
### load (string [ , options ])
4546

46-
Parses `string` as single YAML document. Throws `YAMLException` on error.
47-
This function **does not** understand multi-document and empty sources,
48-
it throws exception on those.
47+
Parses `string` as a single YAML document. Throws `YAMLException` on error.
48+
This function **does not** understand multi-document or empty sources; it throws
49+
an exception on those.
50+
51+
> [!WARNING]
52+
> When processing untrusted input, see the
53+
> [security considerations](docs/safety.md).
4954
5055
options:
5156

5257
- `filename` _(default: null)_ - string to be used as a file path in
5358
error/warning messages.
5459
- `schema` _(default: `CORE_SCHEMA`)_ - specifies a schema to use.
55-
- `FAILSAFE_SCHEMA` - only strings, arrays and plain objects:
56-
- `JSON_SCHEMA` - all JSON-supported types:
57-
- `CORE_SCHEMA` - superset of `JSON_SCHEMA`, accepting more notations for the
58-
same types
60+
- `FAILSAFE_SCHEMA` - only strings, arrays and plain objects.
61+
- `JSON_SCHEMA` - all JSON-supported types.
62+
- `CORE_SCHEMA` - a superset of `JSON_SCHEMA`, accepting more notations for
63+
the same types.
5964
- `YAML11_SCHEMA` - adds the legacy YAML 1.1 types (`!!binary`, `!!timestamp`,
6065
`!!omap`, `!!pairs`, `!!set`, merge keys `<<`, and the broader 1.1 scalar
6166
notations).
62-
- `json` _(default: false)_ - compatibility with JSON.parse behaviour. If true,
63-
then duplicate keys in a mapping will override values rather than throwing an
67+
- `json` _(default: false)_ - compatibility with `JSON.parse` behaviour. If
68+
`true`, duplicate keys in a mapping override values rather than throwing an
6469
error.
65-
- `maxDepth` _(default: 100)_ - limits nesting depth for collections (does not
66-
take aliasees into account).
70+
- `maxDepth` _(default: 100)_ - limits the nesting depth for collections (does
71+
not take aliases into account).
6772
- `maxMergeSeqLength` _(default: 20)_ - limits the number of items in merge
6873
(`<<`) sequences.
6974

70-
NOTE: The default `CORE_SCHEMA` goes without `!!merge` tag. You can easily
71-
enable it if needed:
72-
73-
``` javascript
74-
import { load, CORE_SCHEMA, mergeTag } from 'js-yaml'
75-
76-
load(data, { schema: CORE_SCHEMA.withTags(mergeTag) })
77-
```
75+
> [!NOTE]
76+
>
77+
> The default `CORE_SCHEMA` comes without the `!!merge` tag. You can easily
78+
> enable it if needed:
79+
>
80+
> ``` javascript
81+
> import { load, CORE_SCHEMA, mergeTag } from 'js-yaml'
82+
>
83+
> load(data, { schema: CORE_SCHEMA.withTags(mergeTag) })
84+
> ```
85+
86+
> [!WARNING]
87+
>
88+
> The default `mapTag` is `{}`-object based and does not allow complex keys
89+
> (objects, arrays and so on). That's an intentional choice for convenience.
90+
> Also, non-string scalar keys, such as `null`, numbers or booleans, are
91+
> converted to strings.
92+
>
93+
> In the rare cases where you really need complex keys, use `realMapTag` in the
94+
> schema instead. It stores any key exactly as provided, at the cost of less
95+
> convenient access.
96+
97+
See [examples](examples/) for advanced customization approaches.
7898
7999
80100
### loadAll (string [, options ])
@@ -92,34 +112,46 @@ console.log(loadAll(data))
92112
### dump (object [ , options ])
93113

94114
Serializes `object` as a YAML document. By default it can dump every supported
95-
YAML type, so it will throw an exception if you try to dump regexps or
96-
functions. However, you can disable exceptions by setting the `skipInvalid`
97-
option to `true`.
115+
YAML type, so it throws an exception if you try to dump regexps or functions.
116+
However, you can disable exceptions by setting the `skipInvalid` option to
117+
`true`.
98118

99119
options:
100120

101121
- `indent` _(default: 2)_ - indentation width to use (in spaces).
102122
- `flowLevel` _(default: -1)_ - nesting level at which collections switch from
103123
block to flow style (`-1` means never).
104-
- `seqNoIndent` _(default: false)_ - when true, will not add an indentation level to array elements.
105-
- `seqInlineFirst` _(default: true)_ - when true, allows a nested collection to start on the same line after `-`.
106-
- `skipInvalid` _(default: false)_ - do not throw on invalid types (like function
107-
in the safe schema) and skip pairs and single values with such types.
108-
- `schema` _(default: a `YAML11_SCHEMA`-based schema)_ specifies a schema to use.
124+
- `seqNoIndent` _(default: false)_ - when `true`, does not add an indentation
125+
level to array elements, `␣␣- 1` => `- 1`.
126+
- `seqInlineFirst` _(default: true)_ - when `true`, allows a nested collection
127+
to start on the same line after `-`, `-\n - 1` => `- - 1`.
128+
- `skipInvalid` _(default: false)_ - do not throw on invalid types (such as a
129+
function in the schema). Invalid mapping pairs and sequence items are skipped;
130+
`undefined` sequence items are serialized as `null`.
131+
- `schema` _(default: a `YAML11_SCHEMA`-based schema)_ - specifies a schema to
132+
use.
109133
- `sortKeys` _(default: `false`)_ - if `true`, sort keys when dumping YAML. If a
110134
function, use the function to sort the keys.
111-
- `lineWidth` _(default: `80`)_ - set max line width. Set `-1` for unlimited width.
112-
- `noRefs` _(default: `false`)_ - if `true`, don't convert duplicate objects into references
113-
- `quoteStyle` _(`auto`, `single`, or `double`, default: `auto`)_ - preferred quote style when a scalar needs quotes.
114-
- `flowBracketPadding` _(default: `false`)_ - add spaces inside flow collection brackets.
115-
- `flowSkipCommaSpace` _(default: `false`)_ - omit the space after commas in flow collections.
116-
- `flowSkipColonSpace` _(default: `false`)_ - omit the space after `:` in flow mappings.
117-
- `quoteFlowKeys` _(default: `false`)_ - quote flow mapping keys.
118-
- `tagBeforeAnchor` _(default: `false`)_ - print an explicit tag before an anchor.
135+
- `lineWidth` _(default: `80`)_ - sets the max line width. Set `-1` for unlimited
136+
width.
137+
- `noRefs` _(default: `false`)_ - if `true`, don't convert duplicate objects into
138+
references; inline them instead.
139+
- `quoteStyle` _(`auto`, `single`, or `double`, default: `auto`)_ - force quotes
140+
to single/double, or select the most suitable.
141+
- `flowBracketPadding` _(default: `false`)_ - add spaces inside flow collection
142+
brackets, `{a: 1}` => `{ a: 1 }`.
143+
- `flowSkipCommaSpace` _(default: `false`)_ - omit the space after commas in
144+
flow collections, `[1, 2]` => `[1,2]`.
145+
- `flowSkipColonSpace` _(default: `false`)_ - omit the space after `:` in flow
146+
mappings, `{a: 1}` => `{a:1}`.
147+
- `quoteFlowKeys` _(default: `false`)_ - quote flow mapping keys, `{a: 1}` =>
148+
`{"a": 1}`.
149+
- `tagBeforeAnchor` _(default: `false`)_ - print an explicit tag before an
150+
anchor, `&ref_0 !!set` => `!!set &ref_0`.
119151
- `transform` - a function `(documents: Document[]) => void` that can mutate the
120152
generated AST before it is rendered.
121153

122-
See [examples](examples) for advanced customization approaches.
154+
See [examples](examples/) for advanced customization approaches.
123155

124156

125157
Supported YAML types
@@ -146,7 +178,7 @@ The types below are only available in `YAML11_SCHEMA` (not in the default
146178
!!binary '...base64...' # Uint8Array
147179
!!timestamp 'YYYY-...' # date
148180
!!omap [ ... ] # array of key-value pairs
149-
!!pairs [ ... ] # array or array pairs
181+
!!pairs [ ... ] # array of array pairs
150182
!!set { ... } # Set
151183
```
152184

@@ -156,36 +188,14 @@ See [js-yaml-js-types](https://github.com/nodeca/js-yaml-js-types) for
156188
extra types.
157189

158190

159-
Caveats
160-
-------
161-
162-
By default, `!!map` is loaded as a plain JavaScript object. Scalar keys that are
163-
not strings, such as `null`, numbers or booleans, are converted to strings
164-
because object property keys are strings. Complex keys, such as arrays or
165-
objects, are rejected.
166-
167-
``` yaml
168-
---
169-
? null
170-
: empty
171-
? 1
172-
: number
173-
? true
174-
: boolean
175-
```
176-
177-
``` javascript
178-
{ "null": "empty", "1": "number", "true": "boolean" }
179-
```
180-
181-
182191
CLI
183192
---
184193

185-
This can be useful sometimes for quick-check.
194+
This can be useful sometimes for a quick check.
186195

187196
```
188197
npx js-yaml -h
189198
```
190199

191-
Note, CLI script goes with minimalistic options and without big plans to extend.
200+
Note: the CLI script comes with minimal options, and there are no big plans to
201+
extend it.

0 commit comments

Comments
 (0)