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 21, 2022
1 parent 06a550f commit 4755012
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
5 changes: 4 additions & 1 deletion source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> 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
};
Expand Down
26 changes: 26 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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: {
Expand Down

0 comments on commit 4755012

Please sign in to comment.