Skip to content

Commit 9ec0c70

Browse files
feat: [577] Add yml support (bcherny#588)
* feat: [577] Add yml support * Update README.md Co-authored-by: Boris Cherny <[email protected]> * feat: [577] Add yml support * feat: [577] Rebase - master --------- Co-authored-by: Boris Cherny <[email protected]>
1 parent d97768e commit 9ec0c70

File tree

6 files changed

+64
-7
lines changed

6 files changed

+64
-7
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
[mit]: https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square
66
[node]: https://img.shields.io/badge/Node.js-16+-417e37?style=flat-square
77

8-
> Compile json schema to typescript typings
8+
> Compile json/yaml schema to typescript typings
99
1010
## Example
1111

src/index.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {isDeepStrictEqual} from 'util'
1616
import {link} from './linker'
1717
import {validateOptions} from './optionValidator'
1818
import {JSONSchema as LinkedJSONSchema} from './types/JSONSchema'
19+
import yaml from 'js-yaml'
1920

2021
export {EnumJSONSchema, JSONSchema, NamedEnumJSONSchema, CustomTypeJSONSchema} from './types/JSONSchema'
2122

@@ -112,19 +113,36 @@ export const DEFAULT_OPTIONS: Options = {
112113
unknownAny: true,
113114
}
114115

116+
function isYml(filename: string) {
117+
return filename.endsWith('.yaml') || filename.endsWith('.yml')
118+
}
119+
115120
export function compileFromFile(filename: string, options: Partial<Options> = DEFAULT_OPTIONS): Promise<string> {
116121
const contents = Try(
117122
() => readFileSync(filename),
118123
() => {
119124
throw new ReferenceError(`Unable to read file "${filename}"`)
120125
},
121126
)
122-
const schema = Try<JSONSchema4>(
123-
() => JSON.parse(contents.toString()),
124-
() => {
125-
throw new TypeError(`Error parsing JSON in file "${filename}"`)
126-
},
127-
)
127+
128+
let schema: JSONSchema4
129+
130+
if (isYml(filename)) {
131+
schema = Try<JSONSchema4>(
132+
() => yaml.load(contents.toString()) as JSONSchema4,
133+
() => {
134+
throw new TypeError(`Error parsing YML in file "${filename}"`)
135+
},
136+
)
137+
} else {
138+
schema = Try<JSONSchema4>(
139+
() => JSON.parse(contents.toString()),
140+
() => {
141+
throw new TypeError(`Error parsing JSON in file "${filename}"`)
142+
},
143+
)
144+
}
145+
128146
return compile(schema, stripExtension(filename), {cwd: dirname(filename), ...options})
129147
}
130148

test/__snapshots__/test/test.ts.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449062,6 +449062,34 @@ Generated by [AVA](https://avajs.dev).
449062449062

449063449063
## compileFromFile should resolve refs from cwd option
449064449064

449065+
> Snapshot 1
449066+
449067+
`/* eslint-disable */␊
449068+
/**␊
449069+
* This file was automatically generated by json-schema-to-typescript.␊
449070+
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
449071+
* and run json-schema-to-typescript to regenerate this file.␊
449072+
*/␊
449073+
449074+
export interface Referencing {␊
449075+
foo: ExampleSchema;␊
449076+
}␊
449077+
export interface ExampleSchema {␊
449078+
firstName: string;␊
449079+
lastName: string;␊
449080+
/**␊
449081+
* Age in years␊
449082+
*/␊
449083+
age?: number;␊
449084+
height?: number;␊
449085+
favoriteFoods?: unknown[];␊
449086+
likesDogs?: boolean;␊
449087+
[k: string]: unknown;␊
449088+
}␊
449089+
`
449090+
449091+
## compileFromFile should resolve refs from cwd option as yml
449092+
449065449093
> Snapshot 1
449066449094

449067449095
`/* eslint-disable */␊
16 Bytes
Binary file not shown.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
title: Referencing
2+
type: object
3+
properties:
4+
foo:
5+
"$ref": ReferencedType.json
6+
required:
7+
- foo
8+
additionalProperties: false

test/testCompileFromFile.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,7 @@ import {compileFromFile} from '../src'
44
export function run() {
55
test('compileFromFile should resolve refs from cwd option', async t =>
66
t.snapshot(await compileFromFile('./test/resources/other/ReferencingType.json', {cwd: './test/resources'})))
7+
8+
test('compileFromFile should resolve refs from cwd option as yml', async t =>
9+
t.snapshot(await compileFromFile('./test/resources/other/ReferencingType.yml', {cwd: './test/resources'})))
710
}

0 commit comments

Comments
 (0)