-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
72 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { loadSchema } from './loadSchema' | ||
|
||
test('return the schema when given an object', async () => { | ||
expect(await loadSchema(`test/fixtures/schema.json`)).toMatchInlineSnapshot(` | ||
Object { | ||
"$schema": "http://json-schema.org/draft-07/schema", | ||
"properties": Object { | ||
"$schema": Object { | ||
"description": "Path to my app's schema.", | ||
"type": "string", | ||
}, | ||
"mode": Object { | ||
"default": "\${NODE_ENV}", | ||
"description": "Mode of the app.", | ||
"enum": Array [ | ||
"development", | ||
"test", | ||
"production", | ||
], | ||
"type": "string", | ||
}, | ||
"open": Object { | ||
"default": false, | ||
"description": "Open in a browser tab if true.", | ||
"type": "boolean", | ||
}, | ||
"port": Object { | ||
"default": 3000, | ||
"description": "The port the server listens to.", | ||
"type": "number", | ||
}, | ||
"publicPages": Object { | ||
"default": Array [ | ||
"/hi", | ||
], | ||
"description": "A list of public pages.", | ||
"items": Object { | ||
"type": "string", | ||
}, | ||
"type": "array", | ||
}, | ||
}, | ||
"title": "My awesome app configuration", | ||
"type": "object", | ||
} | ||
`) | ||
}) | ||
|
||
test('load and return the schema when given a string', async () => { | ||
expect(await loadSchema({ foo: 'bar ' })).toMatchInlineSnapshot(` | ||
Object { | ||
"foo": "bar ", | ||
} | ||
`) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { promises as fs } from 'fs' | ||
import { isObject } from 'lodash' | ||
import { JsonObject } from 'type-fest' | ||
|
||
export async function loadSchema( | ||
schema: string | JsonObject | ||
): Promise<JsonObject> { | ||
try { | ||
return isObject(schema) | ||
? schema | ||
: JSON.parse(await fs.readFile(schema, 'utf8')) | ||
} catch (err) { | ||
throw new Error('load: Error loading schema\n' + err.message) | ||
} | ||
} |