mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-20 20:25:48 +00:00
cover api/common/schema-validator.js with tests
This commit is contained in:
@@ -60,4 +60,8 @@ function schemaValidate(schemaName, object) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
SCHEMAS = loadSchemas(path.join(__dirname, './schemas'));
|
SCHEMAS = loadSchemas(path.join(__dirname, './schemas'));
|
||||||
module.exports = schemaValidate;
|
module.exports = {
|
||||||
|
schemaValidate: schemaValidate,
|
||||||
|
loadSchema: loadSchema,
|
||||||
|
SCHEMAS: SCHEMAS
|
||||||
|
};
|
||||||
|
|||||||
@@ -2,7 +2,7 @@
|
|||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const core = require('./utils').core;
|
const core = require('./utils').core;
|
||||||
const ValidationError = require('./errors').ValidationError;
|
const ValidationError = require('./errors').ValidationError;
|
||||||
const schemaValidate = require('./schema-validator');
|
const schemaValidate = require('./schema-validator').schemaValidate;
|
||||||
|
|
||||||
function error(text) {
|
function error(text) {
|
||||||
return new ValidationError(text);
|
return new ValidationError(text);
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
const _ = require('lodash');
|
const _ = require('lodash');
|
||||||
const assert = require('assert-diff');
|
const assert = require('assert-diff');
|
||||||
|
const path = require('path');
|
||||||
const setupAPI = require('./setup-api');
|
const setupAPI = require('./setup-api');
|
||||||
const fixtures = require('./fixtures/api');
|
const fixtures = require('./fixtures/api');
|
||||||
const requests = fixtures.requests;
|
const requests = fixtures.requests;
|
||||||
@@ -15,7 +16,7 @@ const validate = require('../src/api/common/validate');
|
|||||||
const RippleError = require('../src/core/rippleerror').RippleError;
|
const RippleError = require('../src/core/rippleerror').RippleError;
|
||||||
const utils = require('../src/api/ledger/utils');
|
const utils = require('../src/api/ledger/utils');
|
||||||
const ledgerClosed = require('./fixtures/api/rippled/ledger-close-newer');
|
const ledgerClosed = require('./fixtures/api/rippled/ledger-close-newer');
|
||||||
const schemaValidate = require('../src/api/common/schema-validator');
|
const schemaValidator = require('../src/api/common/schema-validator');
|
||||||
|
|
||||||
const orderbook = {
|
const orderbook = {
|
||||||
base: {
|
base: {
|
||||||
@@ -36,7 +37,7 @@ function checkResult(expected, schemaName, done, error, response) {
|
|||||||
// console.log(JSON.stringify(response, null, 2));
|
// console.log(JSON.stringify(response, null, 2));
|
||||||
assert.deepEqual(response, expected);
|
assert.deepEqual(response, expected);
|
||||||
if (schemaName) {
|
if (schemaName) {
|
||||||
schemaValidate(schemaName, response);
|
schemaValidator.schemaValidate(schemaName, response);
|
||||||
}
|
}
|
||||||
done();
|
done();
|
||||||
}
|
}
|
||||||
@@ -154,7 +155,7 @@ describe('RippleAPI', function() {
|
|||||||
withDeterministicPRNG(() => {
|
withDeterministicPRNG(() => {
|
||||||
const result = this.api.sign(requests.sign, secret);
|
const result = this.api.sign(requests.sign, secret);
|
||||||
assert.deepEqual(result, responses.sign);
|
assert.deepEqual(result, responses.sign);
|
||||||
schemaValidate('sign', result);
|
schemaValidator.schemaValidate('sign', result);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -571,6 +572,46 @@ describe('RippleAPI', function() {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('schema-validator', function() {
|
||||||
|
beforeEach(function() {
|
||||||
|
const schema = schemaValidator.loadSchema(path.join(__dirname,
|
||||||
|
'./fixtures/schemas/ledgerhash.json'));
|
||||||
|
schemaValidator.SCHEMAS.ledgerhash = schema;
|
||||||
|
});
|
||||||
|
|
||||||
|
it('valid', function() {
|
||||||
|
assert.doesNotThrow(function() {
|
||||||
|
schemaValidator.schemaValidate('ledgerhash',
|
||||||
|
'0F7ED9F40742D8A513AE86029462B7A6768325583DF8EE21B7EC663019DD6A0F');
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invalid', function() {
|
||||||
|
assert.throws(function() {
|
||||||
|
schemaValidator.schemaValidate('ledgerhash', 'invalid');
|
||||||
|
}, this.api.errors.ValidationError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('invalid - empty value', function() {
|
||||||
|
assert.throws(function() {
|
||||||
|
schemaValidator.schemaValidate('ledgerhash', '');
|
||||||
|
}, this.api.errors.ValidationError);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('load schema error', function() {
|
||||||
|
assert.throws(function() {
|
||||||
|
schemaValidator.loadSchema('/bad/file/name');
|
||||||
|
}, Error);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('schema not found error', function() {
|
||||||
|
assert.throws(function() {
|
||||||
|
schemaValidator.schemaValidate('unexisting', 'anything');
|
||||||
|
}, /schema not found/);
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
it('validator', function() {
|
it('validator', function() {
|
||||||
const noSecret = {address: address};
|
const noSecret = {address: address};
|
||||||
assert.throws(_.partial(validate.addressAndSecret, noSecret),
|
assert.throws(_.partial(validate.addressAndSecret, noSecret),
|
||||||
|
|||||||
7
test/fixtures/schemas/ledgerhash.json
vendored
Normal file
7
test/fixtures/schemas/ledgerhash.json
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"$schema": "http://json-schema.org/draft-04/schema#",
|
||||||
|
"title": "ledgerhash",
|
||||||
|
"description": "A ledger hash",
|
||||||
|
"type": "string",
|
||||||
|
"format": "ledgerHash"
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user