Skip to content

Commit 9921f7a

Browse files
Geraint Corneumcollina
authored andcommitted
feat: ignore unknown formats when compiling schemas (#116)
This allows schemas that may have custom formats to be used. As part of this change I've disabled warning log messages as when setting `unknownFormats: "ignore"` warning messages are emitted everytime a schema with unknown formats is compiled. I have not disabled standard or error log messages.
1 parent b2ec771 commit 9921f7a

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

index.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,18 @@ var merge = require('deepmerge')
66
// This Ajv instance is used to validate that the passed schema
77
// is valid json schema. We reuse the instance to avoid having to
88
// pay the ajv creation cost more than once.
9-
var ajv = new Ajv()
9+
var ajv = new Ajv({
10+
// Ignore any unknown formats as they aren't used.
11+
unknownFormats: 'ignore',
12+
13+
// Ignoring unknown formats emits warnings, but we don't need to hear about
14+
// them.
15+
logger: {
16+
log: console.log,
17+
warn: function () {},
18+
error: console.error
19+
}
20+
})
1021

1122
var uglify = null
1223
var isLong

test/unknownFormats.test.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict'
2+
3+
const test = require('tap').test
4+
const build = require('..')
5+
6+
test('object with custom format field', (t) => {
7+
t.plan(1)
8+
9+
const schema = {
10+
title: 'object with custom format field',
11+
type: 'object',
12+
properties: {
13+
str: {
14+
type: 'string',
15+
format: 'test-format'
16+
}
17+
}
18+
}
19+
20+
const stringify = build(schema)
21+
22+
try {
23+
stringify({
24+
str: 'string'
25+
})
26+
27+
t.pass()
28+
} catch (e) {
29+
t.fail()
30+
}
31+
})

0 commit comments

Comments
 (0)