Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [577] Add yml support #588

Merged
merged 4 commits into from
Apr 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'})))
}
Loading