Skip to content

Commit 47173ed

Browse files
srmarjanimcollina
authored andcommitted
options of ajv is added to options (#150)
1 parent d1677f7 commit 47173ed

File tree

2 files changed

+68
-4
lines changed

2 files changed

+68
-4
lines changed

index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ function build (schema, options) {
110110
var dependencies = []
111111
var dependenciesName = []
112112
if (hasAnyOf(schema) || hasSchemaSomeIf) {
113-
dependencies.push(new Ajv())
113+
dependencies.push(new Ajv(options.ajv))
114114
dependenciesName.push('ajv')
115115
}
116116

test/anyof.test.js

Lines changed: 67 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ test('object with field of type object or null', (t) => {
7474

7575
try {
7676
const value = stringify({
77-
prop: { str: 'string' }
77+
prop: {
78+
str: 'string'
79+
}
7880
})
7981
t.is(value, '{"prop":{"str":"string"}}')
8082
} catch (e) {
@@ -96,7 +98,9 @@ test('object with field of type object or array', (t) => {
9698
additionalProperties: true
9799
}, {
98100
type: 'array',
99-
items: { type: 'string' }
101+
items: {
102+
type: 'string'
103+
}
100104
}]
101105
}
102106
}
@@ -105,7 +109,9 @@ test('object with field of type object or array', (t) => {
105109

106110
try {
107111
const value = stringify({
108-
prop: { str: 'string' }
112+
prop: {
113+
str: 'string'
114+
}
109115
})
110116
t.is(value, '{"prop":{"str":"string"}}')
111117
} catch (e) {
@@ -121,3 +127,61 @@ test('object with field of type object or array', (t) => {
121127
t.fail()
122128
}
123129
})
130+
131+
test('object with field of type string and coercion disable ', (t) => {
132+
t.plan(1)
133+
134+
const schema = {
135+
title: 'object with field of type string',
136+
type: 'object',
137+
properties: {
138+
str: {
139+
anyOf: [{
140+
type: 'string'
141+
}]
142+
}
143+
}
144+
}
145+
const stringify = build(schema)
146+
147+
try {
148+
const value = stringify({
149+
str: 1
150+
})
151+
t.is(value, '{"str":null}')
152+
} catch (e) {
153+
t.fail()
154+
}
155+
})
156+
157+
test('object with field of type string and coercion enable ', (t) => {
158+
t.plan(1)
159+
160+
const schema = {
161+
title: 'object with field of type string',
162+
type: 'object',
163+
properties: {
164+
str: {
165+
anyOf: [{
166+
type: 'string'
167+
}]
168+
}
169+
}
170+
}
171+
172+
const options = {
173+
ajv: {
174+
coerceTypes: true
175+
}
176+
}
177+
const stringify = build(schema, options)
178+
179+
try {
180+
const value = stringify({
181+
str: 1
182+
})
183+
t.is(value, '{"str":"1"}')
184+
} catch (e) {
185+
t.fail()
186+
}
187+
})

0 commit comments

Comments
 (0)