Skip to content

Commit 0d738db

Browse files
committed
fix #86
1 parent 58631c0 commit 0d738db

File tree

5 files changed

+40
-5
lines changed

5 files changed

+40
-5
lines changed

.npmignore

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
npm-debug.log
44
node_modules
55
*.map
6-
example/*
7-
test/*
6+
example
7+
test
8+
dist/test
89
circle.yml
910
ARCHITECTURE.md
1011
CONTRIBUTING.md

src/normalizer.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { JSONSchema, NormalizedJSONSchema } from './types/JSONSchema'
44
import { justName, log, mapDeep, toSafeString } from './utils'
55
import stringify = require('json-stringify-safe')
66

7-
type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName: string) => JSONSchema
7+
type Rule = (schema: JSONSchema, rootSchema: JSONSchema, fileName?: string) => JSONSchema
88
const rules = new Map<string, Rule>()
99

1010
rules.set('Destructure unary types', schema => {
@@ -43,7 +43,7 @@ rules.set('Default top level `id`', (schema, rootSchema, fileName) => {
4343
return schema
4444
})
4545

46-
export function normalize(schema: JSONSchema, filename: string): NormalizedJSONSchema {
46+
export function normalize(schema: JSONSchema, filename?: string): NormalizedJSONSchema {
4747
let _schema = cloneDeep(schema) as NormalizedJSONSchema
4848
rules.forEach((rule, key) => {
4949
_schema = mapDeep(_schema, schema => rule(schema, _schema, filename)) as NormalizedJSONSchema

src/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function mapDeep(
3535
/**
3636
* Eg. `foo/bar/baz.json` => `baz`
3737
*/
38-
export function justName(filename: string): string {
38+
export function justName(filename = ''): string {
3939
return stripExtension(basename(filename))
4040
}
4141

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"title": "Example Schema",
3+
"type": "object",
4+
"properties": {
5+
"firstName": {
6+
"type": "string"
7+
},
8+
"lastName": {
9+
"type": "string"
10+
},
11+
"age": {
12+
"description": "Age in years",
13+
"type": "integer",
14+
"minimum": 0
15+
},
16+
"height": {
17+
"type": "number"
18+
},
19+
"favoriteFoods": {
20+
"type": "array"
21+
},
22+
"likesDogs": {
23+
"type": "boolean"
24+
}
25+
},
26+
"required": ["firstName", "lastName"]
27+
}

test/testCLI.ts

+7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ export function run() {
2525
)
2626
})
2727

28+
test('pipe in (schema without ID), pipe out', t => {
29+
t.is(
30+
execSync('cat ./test/resources/ReferencedTypeWithoutID.json | node dist/src/cli.js', { encoding: 'utf-8' }).toString(),
31+
expected
32+
)
33+
})
34+
2835
test('file in (no flags), pipe out', t => {
2936
t.is(
3037
execSync('node dist/src/cli.js ./test/resources/ReferencedType.json').toString(),

0 commit comments

Comments
 (0)