Skip to content

Commit

Permalink
feat: [577] Add yml support
Browse files Browse the repository at this point in the history
  • Loading branch information
alexanderkrum committed Apr 10, 2024
1 parent 6adcad9 commit 9e9d7c1
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
30 changes: 24 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

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

function isYml(filename: string) {
return filename.split('.').pop() === '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>(
() => JSON.parse(JSON.stringify(yaml.load(contents.toString()))),
() => {
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 @@ -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 */␊
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 9e9d7c1

Please sign in to comment.