Skip to content

Commit

Permalink
Adding the JSON Schema validators for draft-04 and draft-06.
Browse files Browse the repository at this point in the history
  • Loading branch information
khalidx committed Jul 24, 2017
1 parent 83bb2e9 commit 5604a67
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/content/handler/jsonschema/draft-04/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as ajv from 'ajv'

export function validate (content: string): Promise<void> {
return Promise
.resolve()
.then(function () {
let data: string
try {
data = JSON.parse(content)
} catch (e) {
throw new Error('Unable to parse string into JSON.')
}
let validator = new ajv()
validator.addMetaSchema(require('ajv/lib/refs/json-schema-draft-04.json'))
validator.validateSchema(data)
if (validator.errors && validator.errors.length > 0) {
throw new Error(validator.errorsText())
}
return Promise.resolve()
})
.catch(function (error) {
console.error(error)
throw new Error('Schema is invalid!')
})
}
24 changes: 24 additions & 0 deletions src/content/handler/jsonschema/draft-06/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as ajv from 'ajv'

export function validate (content: string): Promise<void> {
return Promise
.resolve()
.then(function () {
let data: string
try {
data = JSON.parse(content)
} catch (e) {
throw new Error('Unable to parse string into JSON.')
}
let validator = new ajv()
validator.validateSchema(data)
if (validator.errors && validator.errors.length > 0) {
throw new Error(validator.errorsText())
}
return Promise.resolve()
})
.catch(function (error) {
console.error(error)
throw new Error('Schema is invalid!')
})
}

0 comments on commit 5604a67

Please sign in to comment.