-
-
Notifications
You must be signed in to change notification settings - Fork 872
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
async schemas and async formats using generators, #40
- Loading branch information
1 parent
a8a7b2d
commit 7a0e4fb
Showing
9 changed files
with
198 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
'use strict'; | ||
|
||
var isBrowser = typeof window == 'object'; | ||
try { eval("(function*(){})()"); var hasGenerators = true; } catch(e){} | ||
var skipTest = isBrowser || !hasGenerators; | ||
|
||
var Ajv = require('./ajv') | ||
, should = require('./chai').should(); | ||
|
||
if (!skipTest) var co = require('' + 'co'); | ||
|
||
|
||
(skipTest ? describe.skip : describe) | ||
('async schemas, formats and keywords', function() { | ||
var ajv, fullAjv; | ||
|
||
beforeEach(function () { | ||
ajv = Ajv(); | ||
fullAjv = Ajv({ allErrors: true }); | ||
}); | ||
|
||
describe('async schemas without async elements', function() { | ||
it('should pass result via callback in setTimeout', function() { | ||
var schema = { | ||
$async: true, | ||
type: 'string', | ||
maxLength: 3 | ||
}; | ||
|
||
return Promise.all([ | ||
test(ajv), | ||
test(fullAjv) | ||
]); | ||
|
||
function test(ajv) { | ||
var validate = ajv.compile(schema); | ||
|
||
return Promise.all([ | ||
shouldBeValid( co(validate('abc')) ), | ||
shouldBeInvalid( co(validate('abcd')) ), | ||
shouldBeInvalid( co(validate(1)) ) | ||
]); | ||
} | ||
}); | ||
|
||
it('should fail compilation if async schema is inside sync schema', function() { | ||
var schema = { | ||
properties: { | ||
foo: { | ||
$async: true, | ||
type: 'string', | ||
maxLength: 3 | ||
} | ||
} | ||
}; | ||
|
||
should.throw(function() { | ||
ajv.compile(schema); | ||
}); | ||
|
||
schema.$async = true; | ||
|
||
ajv.compile(schema); | ||
}); | ||
}); | ||
|
||
|
||
describe('async formats', function() { | ||
it('should return promise that resolves as true or rejects with array of errors', function() { | ||
var schema = { | ||
$async: true, | ||
type: 'string', | ||
format: 'english_word', | ||
minimum: 5 | ||
}; | ||
|
||
return Promise.all([ | ||
test(ajv), | ||
test(fullAjv) | ||
]); | ||
|
||
function test(ajv) { | ||
ajv.addFormat('english_word', { | ||
async: true, | ||
validate: checkWordOnServer | ||
}); | ||
|
||
var validate = ajv.compile(schema); | ||
|
||
return Promise.all([ | ||
shouldBeValid( co(validate('tomorrow')) ), | ||
shouldBeInvalid( co(validate('manana')) ), | ||
shouldBeInvalid( co(validate(1)) ) | ||
]); | ||
} | ||
|
||
function checkWordOnServer(str) { | ||
return str == 'tomorrow' ? Promise.resolve(true) | ||
: str == 'manana' ? Promise.resolve(false) | ||
: Promise.reject(new Error('unknown word')); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
|
||
function shouldBeValid(p) { | ||
return p.then(function (valid) { | ||
valid .should.equal(true); | ||
}); | ||
} | ||
|
||
|
||
var SHOULD_BE_INVALID = 'test: should be invalid'; | ||
function shouldBeInvalid(p) { | ||
return p.then(function (valid) { | ||
throw new Error(SHOULD_BE_INVALID); | ||
}) | ||
.catch(function (err) { | ||
if (err.message == SHOULD_BE_INVALID) throw err; | ||
err. should.be.instanceof(Error); | ||
err.errors .should.be.an('array'); | ||
err.validation .should.equal(true); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters