From 092eaec3f3e8a2039a3a14c118c95b6dcc85430a Mon Sep 17 00:00:00 2001 From: Caleb Usadi Date: Mon, 2 Dec 2024 17:25:56 -0500 Subject: [PATCH] Fix #80 --- source/index.ts | 3 +++ source/types.ts | 7 +++++++ test/index.ts | 22 ++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/source/index.ts b/source/index.ts index 692815a..5b2c363 100644 --- a/source/index.ts +++ b/source/index.ts @@ -103,6 +103,7 @@ export default class Conf = Record = Record> = { */ schema?: Schema; + /** + Top-level properties for the schema. + + Requires a `schema` option to be provided, and cannot contain a `properties` field. + */ + rootSchema?: Omit; + /** Name of the config file (without extension). diff --git a/test/index.ts b/test/index.ts index 22a3a8a..af9dd93 100644 --- a/test/index.ts +++ b/test/index.ts @@ -787,6 +787,28 @@ test('schema - validate Conf default', t => { }, {message: 'Config schema violation: `foo` must be string'}); }); +test('schema - rootSchema without schema', t => { + t.throws(() => { + new Conf({ + cwd: temporaryDirectory(), + rootSchema: {}, + }); + }, {message: '`schema` option required to use `rootSchema`.'}); +}); + +test('schema - validate rootSchema', t => { + t.throws(() => { + const config = new Conf({ + cwd: temporaryDirectory(), + schema: {}, + rootSchema: { + additionalProperties: false, + }, + }); + config.set('foo', 'bar'); + }, {message: 'Config schema violation: `` must NOT have additional properties'}); +}); + test('.get() - without dot notation', t => { t.is(t.context.configWithoutDotNotation.get('foo'), undefined); t.is(t.context.configWithoutDotNotation.get('foo', '🐴'), '🐴');