aboutsummaryrefslogtreecommitdiffstats
path: root/test/schema_test.ts
blob: 42a47c3326cb0b125ac3e7081aa7e8597a1befd5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import 'mocha';
import * as _ from 'lodash';
import * as chai from 'chai';
import * as BigNumber from 'bignumber.js';
import promisify = require('es6-promisify');
import {constants} from './utils/constants';
import {SchemaValidator} from '../src/utils/schema_validator';
import {addressSchema, numberSchema} from '../src/schemas/basic_type_schemas';

chai.config.includeStack = true;
const expect = chai.expect;

describe('Schema', () => {
    const validator = new SchemaValidator();
    const batchTestSchema = (testCases: any[], schema: any, shouldFail = false) => {
        _.forEach(testCases, (testCase: any) => {
            expect(validator.validate(testCase, schema).errors).to.be.lengthOf(shouldFail ? 1 : 0);
        });
    };
    describe('#numberSchema', () => {
        describe('number regex', () => {
            it('should validate valid numbers', () => {
                const testCases = ['42', '0', '1.3', '0.2', '00.00'];
                batchTestSchema(testCases, numberSchema);
            });
            it('should fail for invalid numbers', () => {
                const testCases = ['.3', '1.', 'abacaba', 'и', '1..0'];
                batchTestSchema(testCases, numberSchema, true);
            });
        });
    });
    describe('#addressSchema', () => {
        describe('address regex', () => {
            it('should validate valid addresses', () => {
                const testCases = ['0x8b0292B11a196601eD2ce54B665CaFEca0347D42', constants.NULL_ADDRESS];
                batchTestSchema(testCases, addressSchema);
            });
            it('should fail for invalid addresses', () => {
                const testCases = ['0x', '0', '0x00', '0xzzzzzzB11a196601eD2ce54B665CaFEca0347D42'];
                batchTestSchema(testCases, addressSchema, true);
            });
        });
    });
    describe('BigNumber serialization', () => {
        it('should correctly serialize BigNumbers', () => {
            const testCases = {
                '42': '42',
                '0': '0',
                '1.3': '1.3',
                '0.2': '0.2',
                '00.00': '0',
                '.3': '0.3',
            };
            _.forEach(testCases, (serialized: string, input: string) => {
                expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber(input)))
                    .to.be.equal(serialized);
            });
        });
    });
});