From 094ac38fb5df7b6f13a52fd826fe7d7f0ac2bf9e Mon Sep 17 00:00:00 2001 From: Erin Date: Fri, 12 Feb 2021 19:22:37 +1000 Subject: [PATCH] Add ajv-formats when a schema is used (#144) --- package.json | 1 + source/index.ts | 2 ++ test/index.ts | 15 +++++++++++++++ 3 files changed, 18 insertions(+) diff --git a/package.json b/package.json index 15418d6..aa81864 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/source/index.ts b/source/index.ts index 1e6e544..28bb092 100644 --- a/source/index.ts +++ b/source/index.ts @@ -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'); @@ -97,6 +98,7 @@ class Conf = Record> implements I allErrors: true, useDefaults: true }); + ajvFormats(ajv); const schema: JSONSchema = { type: 'object', diff --git a/test/index.ts b/test/index.ts index f913bed..92ca987 100644 --- a/test/index.ts +++ b/test/index.ts @@ -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: {