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