aboutsummaryrefslogtreecommitdiffstats
path: root/test/schema_test.ts
blob: 1203a4cc1a2540341cd35e0392d797c00fe9ffe4 (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
import 'mocha';
import * as chai from 'chai';
import * as BigNumber from 'bignumber.js';
import {numberSchema} from '../src/schemas/order_schemas';
import {SchemaValidator} from '../src/utils/schema_validator';
import promisify = require('es6-promisify');

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

describe('Schema', () => {
    const validator = new SchemaValidator();
    describe('#numberSchema', () => {
        describe('number regex', () => {
            it('should validate valid numbers', () => {
                expect(validator.validate('42' as any, numberSchema).errors).to.be.lengthOf(0);
                expect(validator.validate('0' as any, numberSchema).errors).to.be.lengthOf(0);
                expect(validator.validate('1.3' as any, numberSchema).errors).to.be.lengthOf(0);
                expect(validator.validate('0.2' as any, numberSchema).errors).to.be.lengthOf(0);
                expect(validator.validate('00.00' as any, numberSchema).errors).to.be.lengthOf(0);
            });
            it('should fail for invalid numbers', () => {
                expect(validator.validate('.3' as any, numberSchema).errors).to.be.lengthOf(1);
                expect(validator.validate('1.' as any, numberSchema).errors).to.be.lengthOf(1);
                expect(validator.validate('abacaba' as any, numberSchema).errors).to.be.lengthOf(1);
                expect(validator.validate('и' as any, numberSchema).errors).to.be.lengthOf(1);
                expect(validator.validate('1..0' as any, numberSchema).errors).to.be.lengthOf(1);
            });
        });
    });
    describe('BigNumber serialization', () => {
        it.only('should correctly serialize BigNumbers', async () => {
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('42'))).to.be.equal('42');
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('0'))).to.be.equal('0');
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('1.3'))).to.be.equal('1.3');
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('0.2'))).to.be.equal('0.2');
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('00.00'))).to.be.equal('0');
            expect(SchemaValidator.convertToJSONSchemaCompatibleObject(new BigNumber('.3'))).to.be.equal('0.3');
        });
    });
});