diff --git a/README.md b/README.md index 52db849d..c061e21e 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ [npm]: https://img.shields.io/npm/v/json-schema-to-typescript.svg?style=flat-square [mit]: https://img.shields.io/npm/l/json-schema-to-typescript.svg?style=flat-square -> Compile json schema to typescript typings +> Compile json/yml schema to typescript typings ## Example diff --git a/package.json b/package.json index d9673c7f..441baf33 100644 --- a/package.json +++ b/package.json @@ -57,6 +57,7 @@ "glob": "^7.1.6", "glob-promise": "^4.2.2", "is-glob": "^4.0.3", + "js-yaml": "^4.1.0", "lodash": "^4.17.21", "minimist": "^1.2.6", "mkdirp": "^1.0.4", @@ -67,6 +68,7 @@ "@types/cli-color": "^2.0.2", "@types/glob": "^7.2.0", "@types/is-glob": "^4.0.2", + "@types/js-yaml": "^4.0.9", "@types/minimist": "^1.2.2", "@types/mkdirp": "^1.0.2", "@types/mz": "^2.7.4", diff --git a/src/index.ts b/src/index.ts index 20a078de..712ee182 100644 --- a/src/index.ts +++ b/src/index.ts @@ -15,6 +15,7 @@ import {validate} from './validator' import {isDeepStrictEqual} from 'util' import {link} from './linker' import {validateOptions} from './optionValidator' +import yaml from 'js-yaml' export {EnumJSONSchema, JSONSchema, NamedEnumJSONSchema, CustomTypeJSONSchema} from './types/JSONSchema' @@ -107,6 +108,10 @@ export const DEFAULT_OPTIONS: Options = { unknownAny: true, } +function isYml(filename: string) { + return filename.split('.').pop() === 'yml' +} + export function compileFromFile(filename: string, options: Partial = DEFAULT_OPTIONS): Promise { const contents = Try( () => readFileSync(filename), @@ -114,12 +119,25 @@ export function compileFromFile(filename: string, options: Partial = DE throw new ReferenceError(`Unable to read file "${filename}"`) }, ) - const schema = Try( - () => JSON.parse(contents.toString()), - () => { - throw new TypeError(`Error parsing JSON in file "${filename}"`) - }, - ) + + let schema: JSONSchema4 + + if (isYml(filename)) { + schema = Try( + () => JSON.parse(JSON.stringify(yaml.load(contents.toString()))), + () => { + throw new TypeError(`Error parsing YML in file "${filename}"`) + }, + ) + } else { + schema = Try( + () => JSON.parse(contents.toString()), + () => { + throw new TypeError(`Error parsing JSON in file "${filename}"`) + }, + ) + } + return compile(schema, stripExtension(filename), {cwd: dirname(filename), ...options}) } diff --git a/test/__snapshots__/test/test.ts.md b/test/__snapshots__/test/test.ts.md index 43fcff5a..aa582aeb 100644 --- a/test/__snapshots__/test/test.ts.md +++ b/test/__snapshots__/test/test.ts.md @@ -449052,6 +449052,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 */␊ diff --git a/test/__snapshots__/test/test.ts.snap b/test/__snapshots__/test/test.ts.snap index cae91b34..28de4e55 100644 Binary files a/test/__snapshots__/test/test.ts.snap and b/test/__snapshots__/test/test.ts.snap differ diff --git a/test/resources/other/ReferencingType.yml b/test/resources/other/ReferencingType.yml new file mode 100644 index 00000000..6a3ab6ec --- /dev/null +++ b/test/resources/other/ReferencingType.yml @@ -0,0 +1,8 @@ +title: Referencing +type: object +properties: + foo: + "$ref": ReferencedType.json +required: + - foo +additionalProperties: false diff --git a/test/testCompileFromFile.ts b/test/testCompileFromFile.ts index 23d0b338..5a45a523 100644 --- a/test/testCompileFromFile.ts +++ b/test/testCompileFromFile.ts @@ -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'}))) }