Skip to content

Commit

Permalink
feat: [577] Add yml support (#588)
Browse files Browse the repository at this point in the history
* feat: [577] Add yml support

* Update README.md

Co-authored-by: Boris Cherny <boris@performancejs.com>

* feat: [577] Add yml support

* feat: [577] Rebase - master

---------

Co-authored-by: Boris Cherny <boris@performancejs.com>
  • Loading branch information
alexanderkrum and bcherny committed Apr 21, 2024
1 parent d97768e commit 9ec0c70
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
[mit]: https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square
[node]: https://img.shields.io/badge/Node.js-16+-417e37?style=flat-square

> Compile json schema to typescript typings
> Compile json/yaml schema to typescript typings
## Example

Expand Down
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {isDeepStrictEqual} from 'util'
import {link} from './linker'
import {validateOptions} from './optionValidator'
import {JSONSchema as LinkedJSONSchema} from './types/JSONSchema'
import yaml from 'js-yaml'

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

Expand Down Expand Up @@ -112,19 +113,36 @@ export const DEFAULT_OPTIONS: Options = {
unknownAny: true,
}

function isYml(filename: string) {
return filename.endsWith('.yaml') || filename.endsWith('.yml')
}

export function compileFromFile(filename: string, options: Partial<Options> = DEFAULT_OPTIONS): Promise<string> {
const contents = Try(
() => readFileSync(filename),
() => {
throw new ReferenceError(`Unable to read file "${filename}"`)
},
)
const schema = Try<JSONSchema4>(
() => JSON.parse(contents.toString()),
() => {
throw new TypeError(`Error parsing JSON in file "${filename}"`)
},
)

let schema: JSONSchema4

if (isYml(filename)) {
schema = Try<JSONSchema4>(
() => yaml.load(contents.toString()) as JSONSchema4,
() => {
throw new TypeError(`Error parsing YML in file "${filename}"`)
},
)
} else {
schema = Try<JSONSchema4>(
() => JSON.parse(contents.toString()),
() => {
throw new TypeError(`Error parsing JSON in file "${filename}"`)
},
)
}

return compile(schema, stripExtension(filename), {cwd: dirname(filename), ...options})
}

Expand Down
28 changes: 28 additions & 0 deletions test/__snapshots__/test/test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -449062,6 +449062,34 @@ Generated by [AVA](https://avajs.dev).

## compileFromFile should resolve refs from cwd option

> Snapshot 1

`/* eslint-disable */␊
/**␊
* This file was automatically generated by json-schema-to-typescript.␊
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,␊
* and run json-schema-to-typescript to regenerate this file.␊
*/␊
export interface Referencing {␊
foo: ExampleSchema;␊
}␊
export interface ExampleSchema {␊
firstName: string;␊
lastName: string;␊
/**␊
* Age in years␊
*/␊
age?: number;␊
height?: number;␊
favoriteFoods?: unknown[];␊
likesDogs?: boolean;␊
[k: string]: unknown;␊
}␊
`

## compileFromFile should resolve refs from cwd option as yml

> Snapshot 1

`/* eslint-disable */␊
Expand Down
Binary file modified test/__snapshots__/test/test.ts.snap
Binary file not shown.
8 changes: 8 additions & 0 deletions test/resources/other/ReferencingType.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
title: Referencing
type: object
properties:
foo:
"$ref": ReferencedType.json
required:
- foo
additionalProperties: false
3 changes: 3 additions & 0 deletions test/testCompileFromFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@ import {compileFromFile} from '../src'
export function run() {
test('compileFromFile should resolve refs from cwd option', async t =>
t.snapshot(await compileFromFile('./test/resources/other/ReferencingType.json', {cwd: './test/resources'})))

test('compileFromFile should resolve refs from cwd option as yml', async t =>
t.snapshot(await compileFromFile('./test/resources/other/ReferencingType.yml', {cwd: './test/resources'})))
}

0 comments on commit 9ec0c70

Please sign in to comment.