Skip to content

Validate schema #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,9 @@ jspm_packages

package-lock.json
yarn.lock

# mac files
.DS_Store

# vim swap files
*.swp
8 changes: 6 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ language: node_js
sudo: false
node_js:
- '4'
- '5'
- '6'
- '7'
- '8'
- '9'

notifications:
email:
on_success: never
on_failure: always
11 changes: 11 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var addComma = `

function build (schema, options) {
options = options || {}
isValidSchema(schema, options.schema)
/* eslint no-new-func: "off" */
var code = `
'use strict'
Expand Down Expand Up @@ -600,4 +601,14 @@ function loadUglify () {
}
}

function isValidSchema (schema, externalSchema) {
const ajv = new Ajv()
if (externalSchema) {
Object.keys(externalSchema).forEach(key => {
ajv.addSchema(externalSchema[key], key)
})
}
ajv.compile(schema)
}

module.exports = build
20 changes: 0 additions & 20 deletions test/additionalProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,26 +161,6 @@ test('additionalProperties - array coerce', (t) => {
t.equal('{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}', stringify(obj))
})

test('additionalProperties - throw on unknown type', (t) => {
t.plan(1)
const stringify = build({
title: 'check array coerce',
type: 'object',
properties: {},
additionalProperties: {
type: 'strangetype'
}
})

const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
try {
stringify(obj)
t.fail()
} catch (e) {
t.pass()
}
})

test('nested additionalProperties', (t) => {
t.plan(1)
const stringify = build({
Expand Down
53 changes: 53 additions & 0 deletions test/basic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,3 +226,56 @@ buildTest({
}, {
readonly: true
})

test('Should throw on invalid schema', t => {
t.plan(1)
try {
build({
type: 'Dinosaur',
properties: {
claws: { type: 'sharp' }
}
})
t.fail('should be an invalid schema')
} catch (err) {
t.ok(err)
}
})

test('additionalProperties - throw on unknown type', (t) => {
t.plan(1)

try {
build({
title: 'check array coerce',
type: 'object',
properties: {},
additionalProperties: {
type: 'strangetype'
}
})
t.fail('should be an invalid schema')
} catch (err) {
t.ok(err)
}
})

test('patternProperties - throw on unknown type', (t) => {
t.plan(1)

try {
build({
title: 'check array coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'strangetype'
}
}
})
t.fail('should be an invalid schema')
} catch (err) {
t.ok(err)
}
})
22 changes: 0 additions & 22 deletions test/patternProperties.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,3 @@ test('patternProperties - array coerce', (t) => {
const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
t.equal(stringify(obj), '{"foo":["t","r","u","e"],"ofoo":[],"arrfoo":["1","2"],"objfoo":[]}')
})

test('patternProperties - throw on unknown type', (t) => {
t.plan(1)
const stringify = build({
title: 'check array coerce',
type: 'object',
properties: {},
patternProperties: {
foo: {
type: 'strangetype'
}
}
})

const obj = { foo: 'true', ofoo: 0, arrfoo: [1, 2], objfoo: { tyrion: 'lannister' } }
try {
stringify(obj)
t.fail()
} catch (e) {
t.pass()
}
})