-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathobject-validator_nullable.spec.ts
61 lines (50 loc) · 1.63 KB
/
object-validator_nullable.spec.ts
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
61
import * as chai from 'chai';
import { join } from 'path';
import { Handler } from '../src/handler';
import { ITypeValidationError, ValidationErrorType } from '../src/result';
const expect = chai.expect;
let dir = join(__dirname, 'specs', 'yaml');
let yaml = join(dir, 'swagger.yaml');
let validator = new Handler(yaml, { partialsDir: dir });
describe('ObjectValidator', () => {
it('should invalidate a nullable object when null and option is disabled', (done) => {
let nullableRef: {
nullableRef: {
test: string
}
} = {
nullableRef: null
};
validator.validateModel(nullableRef, 'NullableRef').then(result => {
expect(result.errors).to.lengthOf(1);
let error: ITypeValidationError = result.errors[0];
expect(error.errorType).to.equals(ValidationErrorType.TYPE_MISMATCH);
expect(error.trace).to.length(2);
expect(error.trace[0].stepName).to.equals('NullableRef');
expect(error.trace[1].stepName).to.equals('nullableRef');
expect(error.typeIs).to.equals('null');
expect(error.typeShouldBe).to.equals('object');
done();
})
.catch(err => {
done(new Error(err));
});
});
it('should validate a nullable object when null', (done) => {
let validator = new Handler(yaml, { allowXNullable: true, partialsDir: dir });
let nullableRef: {
nullableRef: {
test: string
}
} = {
nullableRef: null
};
validator.validateModel(nullableRef, 'NullableRef').then(result => {
expect(result.errors).to.empty;
done();
})
.catch(err => {
done(new Error(err));
});
});
});