Skip to content

Commit

Permalink
Update the async example
Browse files Browse the repository at this point in the history
  • Loading branch information
demsking committed Jun 15, 2017
1 parent 2128b14 commit 350a5f3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
40 changes: 26 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,35 +60,47 @@ const axios = require('axios')
const JsonSchemav = require('jsonschemav')

const jsv = new JsonSchemav()
const schema = { type: 'twitter' }
const schema = { type: 'string', isTwitterAccount: true }
const endpoint = 'https://twitter.com/users/username_available'
const validationFn = (value, data) => {
// value is the keyword value
// data.value is the user data

const validationFn = (data) => axios.get(`${endpoint}?username=${data.value}`)
.then((response) => {
if (response.data.valid) {
const message = `The username '${data.value}' does not exists`
if (value === true) {
return axios.get(`${endpoint}?username=${data.value}`)
.then((response) => {
if (response.data.valid) {
const message = `The username '${data.value}' does not exists`
const err = new Error()

return Promise.reject({ keyword: 'notfound', message })
}
err.props = { message }

return Promise.resolve(data.value)
})
throw err
}

return Promise.resolve(data.value)
})
}

jsv.addType('twitter', validationFn)
return true
}

jsv.addKeyword('string', 'isTwitterAccount', validationFn)

const validator = jsv.compile(schema)

validator
.then((instance) => instance.validate('nonexistingac'))
.catch((err) => {
console.error(err)
// [ { keyword: 'notfound',
// message: 'The username \'nonexistingac\' does not exists' } ]
console.error(err.errors)
// [ { message: 'The username \'nonexistingac\' does not exists',
// keyword: 'isTwitterAccount' } ]

})

validator
.then((instance) => instance.validate('demsking'))
.then(() => {
.then((parsedData) => {
console.log('success')
})
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jsonschemav",
"version": "1.0.1",
"version": "1.0.2",
"description": "A simple JSON Schema Validation",
"main": "lib/api.js",
"scripts": {
Expand Down

0 comments on commit 350a5f3

Please sign in to comment.