Skip to content

Commit 253985b

Browse files
committed
fix: Allow for custom schemas in TS types (#325)
1 parent c2de700 commit 253985b

File tree

3 files changed

+22
-17
lines changed

3 files changed

+22
-17
lines changed

src/options.ts

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import type { ParsedNode } from './nodes/Node.js'
55
import type { Pair } from './nodes/Pair.js'
66
import type { Scalar } from './nodes/Scalar.js'
77
import type { LineCounter } from './parse/line-counter.js'
8-
import type { SchemaName } from './schema/Schema.js'
98
import type { Tags } from './schema/tags.js'
109
import type { CollectionTag, ScalarTag } from './schema/types.js'
1110

@@ -112,9 +111,18 @@ export type SchemaOptions = {
112111
/**
113112
* The base schema to use.
114113
*
115-
* Default: `"core"` for YAML 1.2, `"yaml-1.1"` for earlier versions
114+
* The core library has built-in support for the following:
115+
* - `'failsafe'`: A minimal schema that parses all scalars as strings
116+
* - `'core'`: The YAML 1.2 core schema
117+
* - `'json'`: The YAML 1.2 JSON schema, with minimal rules for JSON compatibility
118+
* - `'yaml-1.1'`: The YAML 1.1 schema
119+
*
120+
* If using another (custom) schema, the `customTags` array needs to
121+
* fully define the schema's tags.
122+
*
123+
* Default: `'core'` for YAML 1.2, `'yaml-1.1'` for earlier versions
116124
*/
117-
schema?: SchemaName
125+
schema?: string
118126

119127
/**
120128
* When adding to or stringifying a map, sort the entries.

src/schema/Schema.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@ import { string } from './common/string.js'
77
import { coreKnownTags, getTags } from './tags.js'
88
import type { CollectionTag, ScalarTag } from './types.js'
99

10-
export type SchemaName = 'core' | 'failsafe' | 'json' | 'yaml-1.1'
11-
1210
const sortMapEntriesByKey = (a: Pair<any>, b: Pair<any>) =>
1311
a.key < b.key ? -1 : a.key > b.key ? 1 : 0
1412

1513
export class Schema {
1614
knownTags: Record<string, CollectionTag | ScalarTag>
1715
merge: boolean
18-
name: SchemaName
16+
name: string
1917
sortMapEntries: ((a: Pair, b: Pair) => number) | null
2018
tags: Array<CollectionTag | ScalarTag>;
2119

src/schema/tags.ts

+10-11
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ import { pairs } from './yaml-1.1/pairs.js'
1414
import { schema as yaml11 } from './yaml-1.1/schema.js'
1515
import { set } from './yaml-1.1/set.js'
1616
import { floatTime, intTime, timestamp } from './yaml-1.1/timestamp.js'
17-
import type { SchemaName } from './Schema.js'
1817
import type { CollectionTag, ScalarTag } from './types.js'
1918

20-
const schemas = {
21-
core,
22-
failsafe: [map, seq, string],
23-
json,
24-
yaml11,
25-
'yaml-1.1': yaml11
26-
}
19+
const schemas = new Map<string, Array<CollectionTag | ScalarTag>>([
20+
['core', core],
21+
['failsafe', [map, seq, string]],
22+
['json', json],
23+
['yaml11', yaml11],
24+
['yaml-1.1', yaml11]
25+
])
2726

2827
const tagsByName = {
2928
binary,
@@ -59,13 +58,13 @@ export const coreKnownTags = {
5958

6059
export function getTags(
6160
customTags: SchemaOptions['customTags'] | undefined,
62-
schemaName: SchemaName
61+
schemaName: string
6362
) {
64-
let tags: Tags = schemas[schemaName]
63+
let tags: Tags | undefined = schemas.get(schemaName)
6564
if (!tags) {
6665
if (Array.isArray(customTags)) tags = []
6766
else {
68-
const keys = Object.keys(schemas)
67+
const keys = Array.from(schemas.keys())
6968
.filter(key => key !== 'yaml11')
7069
.map(key => JSON.stringify(key))
7170
.join(', ')

0 commit comments

Comments
 (0)