From 4755012f781e88379e35bcca6c655fc08ba9d2c6 Mon Sep 17 00:00:00 2001 From: dawidgarus Date: Mon, 21 Mar 2022 21:50:11 +0100 Subject: [PATCH] Feature: Allow schema to be free-form #80 --- source/index.ts | 5 ++++- test/index.ts | 26 ++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/source/index.ts b/source/index.ts index e3f2227..c477633 100644 --- a/source/index.ts +++ b/source/index.ts @@ -119,7 +119,10 @@ class Conf = Record> implements I }); ajvFormats(ajv); - const schema: JSONSchema = isObjectJSONSchema(options.schema) ? options.schema : { + const schema: JSONSchema = isObjectJSONSchema(options.schema) ? { + properties: {}, + ...options.schema + } : { type: 'object', properties: options.schema }; diff --git a/test/index.ts b/test/index.ts index 5d59707..6f667e6 100644 --- a/test/index.ts +++ b/test/index.ts @@ -726,6 +726,19 @@ test('schema - valid set with full schema', t => { }); }); +test('schema - valid set with additionalProps', t => { + const schema: Schema<{foo: {bar: number; foobar: number}}> = { + type: 'object', + additionalProperties: { + type: 'string' + } + }; + const config = new Conf({cwd: tempy.directory(), schema}); + t.notThrows(() => { + config.set('foo', 'string'); + }); +}); + test('schema - one violation', t => { const config = new Conf({ cwd: tempy.directory(), @@ -740,6 +753,19 @@ test('schema - one violation', t => { }, {message: 'Config schema violation: `foo` must be string'}); }); +test('schema - one violation with additionalProperties', t => { + const schema: Schema<{foo: {bar: number; foobar: number}}> = { + type: 'object', + additionalProperties: { + type: 'string' + } + }; + const config = new Conf({cwd: tempy.directory(), schema}); + t.throws(() => { + config.set('foo', 1); + }, {message: 'Config schema violation: `foo` must be string'}); +}); + test('schema - multiple violations', t => { const schema: Schema<{foo: {bar: number; foobar: number}}> = { foo: {