Skip to content

Commit

Permalink
Feature: Allow schema to be free-form sindresorhus#80
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidgarus committed Mar 22, 2022
1 parent 4755012 commit 05faad3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
4 changes: 4 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
throw new TypeError('The `schema` option must be an object.');
}

if ('type' in options.schema && options.schema.type !== 'object') {
throw new TypeError('The `schema.type` property must be set to `object` if present.');
}

const ajv = new Ajv({
allErrors: true,
useDefaults: true
Expand Down
18 changes: 18 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,24 @@ test('schema - should be an object', t => {
}, {message: 'The `schema` option must be an object.'});
});

test('schema - should be a json schema representation of object type', t => {
const schema: any = {
type: 'object'
};
t.notThrows(() => {
new Conf({cwd: tempy.directory(), schema});
});
});

test('schema - should not be a json schema representation of non-object type', t => {
const schema: any = {
type: 'string'
};
t.throws(() => {
new Conf({cwd: tempy.directory(), schema});
}, {message: 'The `schema.type` property must be set to `object` if present.'});
});

test('schema - valid set', t => {
const schema: Schema<{foo: {bar: number; foobar: number}}> = {
foo: {
Expand Down

0 comments on commit 05faad3

Please sign in to comment.