Skip to content

Commit

Permalink
Add ajv-formats when a schema is used (#144)
Browse files Browse the repository at this point in the history
  • Loading branch information
ezzatron authored Feb 12, 2021
1 parent 8043943 commit 094ac38
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
],
"dependencies": {
"ajv": "^7.0.3",
"ajv-formats": "^1.5.1",
"atomically": "^1.7.0",
"debounce-fn": "^4.0.0",
"dot-prop": "^6.0.1",
Expand Down
2 changes: 2 additions & 0 deletions source/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import pkgUp = require('pkg-up');
import envPaths = require('env-paths');
import atomically = require('atomically');
import Ajv, {ValidateFunction as AjvValidateFunction} from 'ajv';
import ajvFormats from 'ajv-formats';
import debounceFn = require('debounce-fn');
import semver = require('semver');
import onetime = require('onetime');
Expand Down Expand Up @@ -97,6 +98,7 @@ class Conf<T extends Record<string, any> = Record<string, unknown>> implements I
allErrors: true,
useDefaults: true
});
ajvFormats(ajv);

const schema: JSONSchema = {
type: 'object',
Expand Down
15 changes: 15 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,21 @@ test('schema - complex schema', t => {
}, {message: 'Config schema violation: `bar` should NOT have more than 3 items; `bar/3` should be integer; `bar` should NOT have duplicate items (items ## 1 and 0 are identical)'});
});

test('schema - supports formats', t => {
const config = new Conf({
cwd: tempy.directory(),
schema: {
foo: {
type: 'string',
format: 'uri'
}
}
});
t.throws(() => {
config.set('foo', 'bar');
}, {message: 'Config schema violation: `foo` should match format "uri"'});
});

test('schema - invalid write to config file', t => {
const schema: Schema<{foo: string}> = {
foo: {
Expand Down

0 comments on commit 094ac38

Please sign in to comment.