Skip to content

Commit d86f933

Browse files
authored
Merge pull request #56 from fastify/validate-schema
Validate schema
2 parents 25ba9c4 + 102b84c commit d86f933

File tree

6 files changed

+76
-44
lines changed

6 files changed

+76
-44
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,9 @@ jspm_packages
3838

3939
package-lock.json
4040
yarn.lock
41+
42+
# mac files
43+
.DS_Store
44+
45+
# vim swap files
46+
*.swp

.travis.yml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@ language: node_js
22
sudo: false
33
node_js:
44
- '4'
5-
- '5'
65
- '6'
7-
- '7'
86
- '8'
7+
- '9'
8+
9+
notifications:
10+
email:
11+
on_success: never
12+
on_failure: always

index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ var addComma = `
2020

2121
function build (schema, options) {
2222
options = options || {}
23+
isValidSchema(schema, options.schema)
2324
/* eslint no-new-func: "off" */
2425
var code = `
2526
'use strict'
@@ -599,4 +600,14 @@ function loadUglify () {
599600
}
600601
}
601602

603+
function isValidSchema (schema, externalSchema) {
604+
const ajv = new Ajv()
605+
if (externalSchema) {
606+
Object.keys(externalSchema).forEach(key => {
607+
ajv.addSchema(externalSchema[key], key)
608+
})
609+
}
610+
ajv.compile(schema)
611+
}
612+
602613
module.exports = build

test/additionalProperties.test.js

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -161,26 +161,6 @@ test('additionalProperties - array coerce', (t) => {
161161
t.equal('{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}', stringify(obj))
162162
})
163163

164-
test('additionalProperties - throw on unknown type', (t) => {
165-
t.plan(1)
166-
const stringify = build({
167-
title: 'check array coerce',
168-
type: 'object',
169-
properties: {},
170-
additionalProperties: {
171-
type: 'strangetype'
172-
}
173-
})
174-
175-
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
176-
try {
177-
stringify(obj)
178-
t.fail()
179-
} catch (e) {
180-
t.pass()
181-
}
182-
})
183-
184164
test('nested additionalProperties', (t) => {
185165
t.plan(1)
186166
const stringify = build({

test/basic.test.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,3 +226,56 @@ buildTest({
226226
}, {
227227
readonly: true
228228
})
229+
230+
test('Should throw on invalid schema', t => {
231+
t.plan(1)
232+
try {
233+
build({
234+
type: 'Dinosaur',
235+
properties: {
236+
claws: { type: 'sharp' }
237+
}
238+
})
239+
t.fail('should be an invalid schema')
240+
} catch (err) {
241+
t.ok(err)
242+
}
243+
})
244+
245+
test('additionalProperties - throw on unknown type', (t) => {
246+
t.plan(1)
247+
248+
try {
249+
build({
250+
title: 'check array coerce',
251+
type: 'object',
252+
properties: {},
253+
additionalProperties: {
254+
type: 'strangetype'
255+
}
256+
})
257+
t.fail('should be an invalid schema')
258+
} catch (err) {
259+
t.ok(err)
260+
}
261+
})
262+
263+
test('patternProperties - throw on unknown type', (t) => {
264+
t.plan(1)
265+
266+
try {
267+
build({
268+
title: 'check array coerce',
269+
type: 'object',
270+
properties: {},
271+
patternProperties: {
272+
foo: {
273+
type: 'strangetype'
274+
}
275+
}
276+
})
277+
t.fail('should be an invalid schema')
278+
} catch (err) {
279+
t.ok(err)
280+
}
281+
})

test/patternProperties.test.js

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -137,25 +137,3 @@ test('patternProperties - array coerce', (t) => {
137137
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
138138
t.equal(stringify(obj), '{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}')
139139
})
140-
141-
test('patternProperties - throw on unknown type', (t) => {
142-
t.plan(1)
143-
const stringify = build({
144-
title: 'check array coerce',
145-
type: 'object',
146-
properties: {},
147-
patternProperties: {
148-
foo: {
149-
type: 'strangetype'
150-
}
151-
}
152-
})
153-
154-
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
155-
try {
156-
stringify(obj)
157-
t.fail()
158-
} catch (e) {
159-
t.pass()
160-
}
161-
})

0 commit comments

Comments
 (0)