UNPKG

4.52 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var tslib_1 = require("tslib");
4var test_1 = require("../test");
5var __1 = require("..");
6var SchemaDefinition_1 = require("./SchemaDefinition");
7var BASE = test_1.fsPath.resolve('./src/example');
8describe('Schema', function () {
9 var _this = this;
10 this.timeout(5000);
11 var schema = __1.Schema.compile({ files: 'types/index.ts', basePath: BASE });
12 describe('construction', function () {
13 it('resolves relative file paths', function () {
14 var path = test_1.fsPath.resolve('./src/example/types/types.other.ts');
15 var schema = __1.Schema.compile({
16 files: ['./src/example/types/types.ts', path],
17 });
18 test_1.expect(schema.basePath).to.eql(undefined);
19 test_1.expect(schema.files[0]).to.eql(test_1.fsPath.join(BASE, 'types/types.ts'));
20 test_1.expect(schema.files[1]).to.eql(path);
21 });
22 it('resolves base path', function () {
23 var schema = __1.Schema.compile({
24 basePath: './src/example',
25 files: '/types/index.ts',
26 });
27 test_1.expect(schema.basePath).to.eql(BASE);
28 test_1.expect(schema.files[0]).to.eql(test_1.fsPath.join(BASE, '/types/index.ts'));
29 });
30 it('throws if files do not exist', function () {
31 var fn = function () { return __1.Schema.compile({ files: '__NOT_EXIST__' }); };
32 test_1.expect(fn).to.throw(/does not exist/);
33 });
34 it('throw if no files are passed', function () {
35 test_1.expect(function () { return __1.Schema.compile({ files: [] }); }).to.throw();
36 test_1.expect(function () { return __1.Schema.compile({ files: '' }); }).to.throw();
37 test_1.expect(function () { return __1.Schema.compile({ files: ' ' }); }).to.throw();
38 test_1.expect(function () { return __1.Schema.compile({ files: ['', ' '] }); }).to.throw();
39 });
40 });
41 describe('schema generation', function () {
42 it('for type (single)', function () {
43 var res = schema.forType('IFoo');
44 var props = res.def.properties;
45 test_1.expect(res.isValidSchema).to.eql(true);
46 test_1.expect(res.def.type).to.eql('object');
47 test_1.expect(props.name.type).to.eql('string');
48 test_1.expect(res.def.required).to.eql(['age', 'name']);
49 });
50 it('for types (plural)', function () {
51 var res = schema.forType(['IFoo', 'IBar']);
52 var defs = res.def.definitions;
53 test_1.expect(res.isValidSchema).to.eql(true);
54 test_1.expect(defs.IFoo.properties.name.type).to.eql('string');
55 test_1.expect(defs.IBar.properties.createdAt.type).to.eql('number');
56 });
57 it('error: no match', function () {
58 var fn1 = function () { return schema.forType('INotExist'); };
59 var fn2 = function () { return schema.forType(['INope', 'INada']); };
60 test_1.expect(fn1).to.throw(/Failed to generate schema for symbol 'INotExist'/);
61 test_1.expect(fn2).to.throw(/Failed to generate schema for symbol 'INope,INada'/);
62 });
63 it('is not a valid schema', function () {
64 var bogus = 'hello';
65 var def = new SchemaDefinition_1.SchemaDefinition({ type: 'IFoo', def: bogus });
66 test_1.expect(def.isValidSchema).to.eql(false);
67 });
68 it('schemaVersion', function () {
69 var res = schema.forType('IFoo');
70 test_1.expect(res.schemaVersion).to.eql('http://json-schema.org/draft-07/schema#');
71 });
72 });
73 describe('validate', function () {
74 var foo = schema.forType('IFoo');
75 it('is valid', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
76 var res;
77 return tslib_1.__generator(this, function (_a) {
78 res = foo.validate({ name: 'Sky', age: 30 });
79 test_1.expect(res).to.eql(true);
80 return [2];
81 });
82 }); });
83 it('is not valid', function () { return tslib_1.__awaiter(_this, void 0, void 0, function () {
84 var res;
85 return tslib_1.__generator(this, function (_a) {
86 res = foo.validate({ name: true });
87 test_1.expect(res).to.eql(false);
88 return [2];
89 });
90 }); });
91 });
92});
93//# sourceMappingURL=Schema.test.js.map
\No newline at end of file