ac-convert-json-schema-to-mongoose 0.3.4 → 0.3.6

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,146 +0,0 @@
1
- import * as _ from 'lodash';
2
- import * as mongoose from 'mongoose';
3
-
4
- const typeStringToMongooseType = {'string': String, 'boolean': Boolean, 'number': Number, 'integer': Number};
5
-
6
- const typeRefToMongooseType = {
7
- '#/definitions/objectid': mongoose.Schema.Types.ObjectId, '#/definitions/dateOrDatetime': Date
8
- };
9
-
10
- const subSchemaTypeV3 = (parentSchema: any, subschema: any, key: any) => {
11
- return (0 <= parentSchema.required.indexOf(key) && !_.isPlainObject(subschema)) ? {
12
- type: subschema, required: true
13
- } : subschema;
14
- };
15
-
16
- const subSchemaTypeV4 = (parentSchema: any, subschema: any, key: any) => {
17
- return (0 <= parentSchema.required.indexOf(key) ) ? !_.isPlainObject(subschema) ? {
18
- type: subschema, required: true
19
- } : subschema.hasOwnProperty('type') ? _.assign(subschema, {required: true}) : subschema : subschema;
20
- };
21
-
22
- // noinspection ReservedWordAsName
23
- const schemaParamsToMongoose = {
24
- /**
25
- * default value
26
- */
27
- default: (default_: string) => {
28
- const func = (_.last(/^\[Function=(.+)\]$/.exec(default_)) || '')
29
- .replace(/\\_/g, '`underscore`')
30
- .replace(/_/g, ' ')
31
- .replace(/`underscore`/g, '_');
32
-
33
- // noinspection ReservedWordAsName,DynamicallyGeneratedCodeJS
34
- return {default: eval(func) || default_};
35
- },
36
-
37
- /**
38
- * Pattern for value to match
39
- */
40
- pattern: (pattern: string) => ({match: RegExp(pattern)}),
41
- type: (type: string) => ({type: (<any>typeStringToMongooseType)[type]}),
42
- minLength: (min: number) => ({minlength: min}),
43
- maxLength: (max: number) => ({maxlength: max}),
44
- minimum: (min: number) => ({min: min}),
45
- maximum: (max: number) => ({max: max}),
46
- enum: (members: any[]) => ({enum: members})
47
- };
48
-
49
- const toMongooseParams = (acc: any, val: any, key: any) => {
50
- let func;
51
-
52
- // noinspection AssignmentResultUsedJS
53
- return (func = (<any>schemaParamsToMongoose)[key]) ? _.assign(acc, func(val)) : acc;
54
- };
55
-
56
- const unsupportedRefValue = (jsonSchema: any) => {
57
- throw new Error('Unsupported $ref value: ' + jsonSchema.$ref);
58
- };
59
-
60
- const unsupportedJsonSchema = (jsonSchema: any) => {
61
- throw new Error('Unsupported JSON schema type, `' + jsonSchema.type + '`');
62
- };
63
-
64
- const convertV = (version: any, refSchemas: any, jsonSchema: any): any => {
65
-
66
- if (!_.isPlainObject(jsonSchema)) {
67
- unsupportedJsonSchema(jsonSchema);
68
- }
69
-
70
- let converted,
71
- result,
72
- format = jsonSchema.format,
73
- isRef = !_.isEmpty(jsonSchema.$ref),
74
- isTypeDate = ('string' === jsonSchema.type) && (('date' === format) || ('date-time' === format)),
75
- mongooseRef = (<any>typeRefToMongooseType)[jsonSchema.$ref],
76
- isMongooseRef = ('undefined' != typeof(mongooseRef)),
77
- subSchema = _.isEmpty(refSchemas) ? false : refSchemas[jsonSchema.$ref],
78
- subSchemaType = (4 == version) ? subSchemaTypeV4 : subSchemaTypeV3;
79
-
80
- return (result =
81
- isRef ?
82
- isMongooseRef ?
83
- mongooseRef :
84
- subSchema ?
85
- convertV(version, refSchemas, subSchema) :
86
- unsupportedRefValue(jsonSchema)
87
-
88
- :
89
- isTypeDate ?
90
- _.reduce(<any> _.omit(jsonSchema, 'type', 'format'), toMongooseParams, {type: typeRefToMongooseType['#/definitions/dateOrDatetime']})
91
-
92
- :
93
- _.has(typeStringToMongooseType, jsonSchema.type) ?
94
- _.reduce(jsonSchema, toMongooseParams, {})
95
-
96
- :
97
- (jsonSchema.type === 'object') ?
98
- _.isEmpty(jsonSchema.properties) ?
99
- mongoose.Schema.Types.Mixed :
100
- ( converted =
101
- _.mapValues(jsonSchema.properties, convertV.bind(null, version, refSchemas)), jsonSchema.required ?
102
- (_.mapValues(converted, subSchemaType.bind(null, jsonSchema))) :
103
- converted )
104
-
105
- :
106
- (jsonSchema.type === 'array') ?
107
- !_.isEmpty(jsonSchema.items) ?
108
- [convertV(version, refSchemas, jsonSchema.items)] :
109
- []
110
-
111
- :
112
- !_.has(jsonSchema, 'type') ?
113
- mongoose.Schema.Types.Mixed :
114
- unsupportedJsonSchema(jsonSchema)
115
- );
116
- };
117
-
118
- const convert = (refSchemas: any, jsonSchema: any): any => {
119
- let version = 3;
120
-
121
- switch (jsonSchema.$schema) {
122
- case 'http://json-schema.org/draft-03/schema#':
123
- version = 3;
124
- break;
125
-
126
- case 'http://json-schema.org/draft-04/schema#':
127
- version = 4;
128
- break;
129
-
130
- // backwards compatibility
131
- default:
132
- version = 3;
133
- break;
134
- }
135
-
136
- return convertV(version, refSchemas, jsonSchema);
137
- };
138
-
139
- interface CreateMongooseSchema {
140
- (refSchemas: any, jsonSchema: any): any
141
-
142
- (refSchemas: any): (jsonSchema: any) => any
143
- }
144
-
145
- // noinspection JSUnusedGlobalSymbols
146
- export default <CreateMongooseSchema> _.curry(convert);
@@ -1,442 +0,0 @@
1
- // Type definitions for Jasmine 2.1
2
- // Project: http://jasmine.github.io/
3
- // Definitions by: Boris Yankov <https://github.com/borisyankov/>, Theodore Brown <https://github.com/theodorejb>, David Pärsson <https://github.com/davidparsson/>
4
- // Definitions: https://github.com/borisyankov/DefinitelyTyped
5
-
6
-
7
- // For ddescribe / iit use : https://github.com/borisyankov/DefinitelyTyped/blob/master/karma-jasmine/karma-jasmine.d.ts
8
-
9
- declare function describe(description: string, specDefinitions: () => void): void;
10
- declare function fdescribe(description: string, specDefinitions: () => void): void;
11
- declare function xdescribe(description: string, specDefinitions: () => void): void;
12
-
13
- declare function it(expectation: string, assertion?: () => void): void;
14
- declare function it(expectation: string, assertion?: (done: () => void) => void): void;
15
- declare function fit(expectation: string, assertion?: () => void): void;
16
- declare function fit(expectation: string, assertion?: (done: () => void) => void): void;
17
- declare function xit(expectation: string, assertion?: () => void): void;
18
- declare function xit(expectation: string, assertion?: (done: () => void) => void): void;
19
-
20
- /** If you call the function pending anywhere in the spec body, no matter the expectations, the spec will be marked pending. */
21
- declare function pending(): void;
22
-
23
- declare function beforeEach(action: () => void): void;
24
- declare function beforeEach(action: (done: () => void) => void): void;
25
- declare function afterEach(action: () => void): void;
26
- declare function afterEach(action: (done: () => void) => void): void;
27
-
28
- declare function beforeAll(action: () => void): void;
29
- declare function beforeAll(action: (done: () => void) => void): void;
30
- declare function afterAll(action: () => void): void;
31
- declare function afterAll(action: (done: () => void) => void): void;
32
-
33
- declare function expect(spy: Function): jasmine.Matchers;
34
- declare function expect(actual: any): jasmine.Matchers;
35
-
36
- declare function fail(e?: any): void;
37
-
38
- declare function spyOn(object: any, method: string): jasmine.Spy;
39
-
40
- declare function runs(asyncMethod: Function): void;
41
- declare function waitsFor(latchMethod: () => boolean, failureMessage?: string, timeout?: number): void;
42
- declare function waits(timeout?: number): void;
43
-
44
- declare module jasmine {
45
-
46
- var clock: () => Clock;
47
-
48
- function any(aclass: any): Any;
49
- function objectContaining(sample: any): ObjectContaining;
50
- function createSpy(name: string, originalFn?: Function): Spy;
51
- function createSpyObj(baseName: string, methodNames: any[]): any;
52
- function createSpyObj<T>(baseName: string, methodNames: any[]): T;
53
- function pp(value: any): string;
54
- function getEnv(): Env;
55
- function addMatchers(matchers: any): Any;
56
-
57
- interface Any {
58
-
59
- new (expectedClass: any): any;
60
-
61
- jasmineMatches(other: any): boolean;
62
- jasmineToString(): string;
63
- }
64
-
65
- interface ObjectContaining {
66
- new (sample: any): any;
67
-
68
- jasmineMatches(other: any, mismatchKeys: any[], mismatchValues: any[]): boolean;
69
- jasmineToString(): string;
70
- }
71
-
72
- interface Block {
73
-
74
- new (env: Env, func: SpecFunction, spec: Spec): any;
75
-
76
- execute(onComplete: () => void): void;
77
- }
78
-
79
- interface WaitsBlock extends Block {
80
- new (env: Env, timeout: number, spec: Spec): any;
81
- }
82
-
83
- interface WaitsForBlock extends Block {
84
- new (env: Env, timeout: number, latchFunction: SpecFunction, message: string, spec: Spec): any;
85
- }
86
-
87
- interface Clock {
88
- install(): void;
89
- uninstall(): void;
90
- /** Calls to any registered callback are triggered when the clock is ticked forward via the jasmine.clock().tick function, which takes a number of milliseconds. */
91
- tick(ms: number): void;
92
- }
93
-
94
- interface Env {
95
- setTimeout: any;
96
- clearTimeout: void;
97
- setInterval: any;
98
- clearInterval: void;
99
- updateInterval: number;
100
-
101
- currentSpec: Spec;
102
-
103
- matchersClass: Matchers;
104
-
105
- version(): any;
106
- versionString(): string;
107
- nextSpecId(): number;
108
- addReporter(reporter: Reporter): void;
109
- execute(): void;
110
- describe(description: string, specDefinitions: () => void): Suite;
111
- // ddescribe(description: string, specDefinitions: () => void): Suite; Not a part of jasmine. Angular team adds these
112
- beforeEach(beforeEachFunction: () => void): void;
113
- beforeAll(beforeAllFunction: () => void): void;
114
- currentRunner(): Runner;
115
- afterEach(afterEachFunction: () => void): void;
116
- afterAll(afterAllFunction: () => void): void;
117
- xdescribe(desc: string, specDefinitions: () => void): XSuite;
118
- it(description: string, func: () => void): Spec;
119
- // iit(description: string, func: () => void): Spec; Not a part of jasmine. Angular team adds these
120
- xit(desc: string, func: () => void): XSpec;
121
- compareRegExps_(a: RegExp, b: RegExp, mismatchKeys: string[], mismatchValues: string[]): boolean;
122
- compareObjects_(a: any, b: any, mismatchKeys: string[], mismatchValues: string[]): boolean;
123
- equals_(a: any, b: any, mismatchKeys: string[], mismatchValues: string[]): boolean;
124
- contains_(haystack: any, needle: any): boolean;
125
- addEqualityTester(equalityTester: (a: any, b: any, env: Env, mismatchKeys: string[], mismatchValues: string[]) => boolean): void;
126
- specFilter(spec: Spec): boolean;
127
- }
128
-
129
- interface FakeTimer {
130
-
131
- new (): any;
132
-
133
- reset(): void;
134
- tick(millis: number): void;
135
- runFunctionsWithinRange(oldMillis: number, nowMillis: number): void;
136
- scheduleFunction(timeoutKey: any, funcToCall: () => void, millis: number, recurring: boolean): void;
137
- }
138
-
139
- interface HtmlReporter {
140
- new (): any;
141
- }
142
-
143
- interface HtmlSpecFilter {
144
- new (): any;
145
- }
146
-
147
- interface Result {
148
- type: string;
149
- }
150
-
151
- interface NestedResults extends Result {
152
- description: string;
153
-
154
- totalCount: number;
155
- passedCount: number;
156
- failedCount: number;
157
-
158
- skipped: boolean;
159
-
160
- rollupCounts(result: NestedResults): void;
161
- log(values: any): void;
162
- getItems(): Result[];
163
- addResult(result: Result): void;
164
- passed(): boolean;
165
- }
166
-
167
- interface MessageResult extends Result {
168
- values: any;
169
- trace: Trace;
170
- }
171
-
172
- interface ExpectationResult extends Result {
173
- matcherName: string;
174
- passed(): boolean;
175
- expected: any;
176
- actual: any;
177
- message: string;
178
- trace: Trace;
179
- }
180
-
181
- interface Trace {
182
- name: string;
183
- message: string;
184
- stack: any;
185
- }
186
-
187
- interface PrettyPrinter {
188
-
189
- new (): any;
190
-
191
- format(value: any): void;
192
- iterateObject(obj: any, fn: (property: string, isGetter: boolean) => void): void;
193
- emitScalar(value: any): void;
194
- emitString(value: string): void;
195
- emitArray(array: any[]): void;
196
- emitObject(obj: any): void;
197
- append(value: any): void;
198
- }
199
-
200
- interface StringPrettyPrinter extends PrettyPrinter {
201
- }
202
-
203
- interface Queue {
204
-
205
- new (env: any): any;
206
-
207
- env: Env;
208
- ensured: boolean[];
209
- blocks: Block[];
210
- running: boolean;
211
- index: number;
212
- offset: number;
213
- abort: boolean;
214
-
215
- addBefore(block: Block, ensure?: boolean): void;
216
- add(block: any, ensure?: boolean): void;
217
- insertNext(block: any, ensure?: boolean): void;
218
- start(onComplete?: () => void): void;
219
- isRunning(): boolean;
220
- next_(): void;
221
- results(): NestedResults;
222
- }
223
-
224
- interface Matchers {
225
-
226
- new (env: Env, actual: any, spec: Env, isNot?: boolean): any;
227
-
228
- env: Env;
229
- actual: any;
230
- spec: Env;
231
- isNot?: boolean;
232
- message(): any;
233
-
234
- toBe(expected: any): boolean;
235
- toEqual(expected: any): boolean;
236
- toMatch(expected: any): boolean;
237
- toBeDefined(): boolean;
238
- toBeUndefined(): boolean;
239
- toBeNull(): boolean;
240
- toBeNaN(): boolean;
241
- toBeTruthy(): boolean;
242
- toBeFalsy(): boolean;
243
- toHaveBeenCalled(): boolean;
244
- toHaveBeenCalledWith(...params: any[]): boolean;
245
- toContain(expected: any): boolean;
246
- toBeLessThan(expected: any): boolean;
247
- toBeGreaterThan(expected: any): boolean;
248
- toBeCloseTo(expected: any, precision: any): boolean;
249
- toContainHtml(expected: string): boolean;
250
- toContainText(expected: string): boolean;
251
- toThrow(expected?: any): boolean;
252
- toThrowError(expected?: any): boolean;
253
- not: Matchers;
254
-
255
- Any: Any;
256
- }
257
-
258
- interface Reporter {
259
- reportRunnerStarting(runner: Runner): void;
260
- reportRunnerResults(runner: Runner): void;
261
- reportSuiteResults(suite: Suite): void;
262
- reportSpecStarting(spec: Spec): void;
263
- reportSpecResults(spec: Spec): void;
264
- log(str: string): void;
265
- }
266
-
267
- interface MultiReporter extends Reporter {
268
- addReporter(reporter: Reporter): void;
269
- }
270
-
271
- interface Runner {
272
-
273
- new (env: Env): any;
274
-
275
- execute(): void;
276
- beforeEach(beforeEachFunction: SpecFunction): void;
277
- afterEach(afterEachFunction: SpecFunction): void;
278
- beforeAll(beforeAllFunction: SpecFunction): void;
279
- afterAll(afterAllFunction: SpecFunction): void;
280
- finishCallback(): void;
281
- addSuite(suite: Suite): void;
282
- add(block: Block): void;
283
- specs(): Spec[];
284
- suites(): Suite[];
285
- topLevelSuites(): Suite[];
286
- results(): NestedResults;
287
- }
288
-
289
- interface SpecFunction {
290
- (spec?: Spec): void;
291
- }
292
-
293
- interface SuiteOrSpec {
294
- id: number;
295
- env: Env;
296
- description: string;
297
- queue: Queue;
298
- }
299
-
300
- interface Spec extends SuiteOrSpec {
301
-
302
- new (env: Env, suite: Suite, description: string): any;
303
-
304
- suite: Suite;
305
-
306
- afterCallbacks: SpecFunction[];
307
- spies_: Spy[];
308
-
309
- results_: NestedResults;
310
- matchersClass: Matchers;
311
-
312
- getFullName(): string;
313
- results(): NestedResults;
314
- log(arguments: any): any;
315
- runs(func: SpecFunction): Spec;
316
- addToQueue(block: Block): void;
317
- addMatcherResult(result: Result): void;
318
- expect(actual: any): any;
319
- waits(timeout: number): Spec;
320
- waitsFor(latchFunction: SpecFunction, timeoutMessage?: string, timeout?: number): Spec;
321
- fail(e?: any): void;
322
- getMatchersClass_(): Matchers;
323
- addMatchers(matchersPrototype: any): void;
324
- finishCallback(): void;
325
- finish(onComplete?: () => void): void;
326
- after(doAfter: SpecFunction): void;
327
- execute(onComplete?: () => void): any;
328
- addBeforesAndAftersToQueue(): void;
329
- explodes(): void;
330
- spyOn(obj: any, methodName: string, ignoreMethodDoesntExist: boolean): Spy;
331
- removeAllSpies(): void;
332
- }
333
-
334
- interface XSpec {
335
- id: number;
336
- runs(): void;
337
- }
338
-
339
- interface Suite extends SuiteOrSpec {
340
-
341
- new (env: Env, description: string, specDefinitions: () => void, parentSuite: Suite): any;
342
-
343
- parentSuite: Suite;
344
-
345
- getFullName(): string;
346
- finish(onComplete?: () => void): void;
347
- beforeEach(beforeEachFunction: SpecFunction): void;
348
- afterEach(afterEachFunction: SpecFunction): void;
349
- beforeAll(beforeAllFunction: SpecFunction): void;
350
- afterAll(afterAllFunction: SpecFunction): void;
351
- results(): NestedResults;
352
- add(suiteOrSpec: SuiteOrSpec): void;
353
- specs(): Spec[];
354
- suites(): Suite[];
355
- children(): any[];
356
- execute(onComplete?: () => void): void;
357
- }
358
-
359
- interface XSuite {
360
- execute(): void;
361
- }
362
-
363
- interface Spy {
364
- (...params: any[]): any;
365
-
366
- identity: string;
367
- and: SpyAnd;
368
- calls: Calls;
369
- mostRecentCall: { args: any[]; };
370
- argsForCall: any[];
371
- wasCalled: boolean;
372
- callCount: number;
373
- }
374
-
375
- interface SpyAnd {
376
- /** By chaining the spy with and.callThrough, the spy will still track all calls to it but in addition it will delegate to the actual implementation. */
377
- callThrough(): Spy;
378
- /** By chaining the spy with and.returnValue, all calls to the function will return a specific value. */
379
- returnValue(val: any): void;
380
- /** By chaining the spy with and.callFake, all calls to the spy will delegate to the supplied function. */
381
- callFake(fn: Function): Spy;
382
- /** By chaining the spy with and.throwError, all calls to the spy will throw the specified value. */
383
- throwError(msg: string): void;
384
- /** When a calling strategy is used for a spy, the original stubbing behavior can be returned at any time with and.stub. */
385
- stub(): Spy;
386
- }
387
-
388
- interface Calls {
389
- /** By chaining the spy with calls.any(), will return false if the spy has not been called at all, and then true once at least one call happens. **/
390
- any(): boolean;
391
- /** By chaining the spy with calls.count(), will return the number of times the spy was called **/
392
- count(): number;
393
- /** By chaining the spy with calls.argsFor(), will return the arguments passed to call number index **/
394
- argsFor(index: number): any[];
395
- /** By chaining the spy with calls.allArgs(), will return the arguments to all calls **/
396
- allArgs(): any[];
397
- /** By chaining the spy with calls.all(), will return the context (the this) and arguments passed all calls **/
398
- all(): any;
399
- /** By chaining the spy with calls.mostRecent(), will return the context (the this) and arguments for the most recent call **/
400
- mostRecent(): any;
401
- /** By chaining the spy with calls.first(), will return the context (the this) and arguments for the first call **/
402
- first(): any;
403
- /** By chaining the spy with calls.reset(), will clears all tracking for a spy **/
404
- reset(): void;
405
- }
406
-
407
- interface Util {
408
- inherit(childClass: Function, parentClass: Function): any;
409
- formatException(e: any): any;
410
- htmlEscape(str: string): string;
411
- argsToArray(args: any): any;
412
- extend(destination: any, source: any): any;
413
- }
414
-
415
- interface JsApiReporter extends Reporter {
416
-
417
- started: boolean;
418
- finished: boolean;
419
- result: any;
420
- messages: any;
421
-
422
- new (): any;
423
-
424
- suites(): Suite[];
425
- summarize_(suiteOrSpec: SuiteOrSpec): any;
426
- results(): any;
427
- resultsForSpec(specId: any): any;
428
- log(str: any): any;
429
- resultsForSpecs(specIds: any): any;
430
- summarizeResult_(result: any): any;
431
- }
432
-
433
- interface Jasmine {
434
- Spec: Spec;
435
- clock: Clock;
436
- util: Util;
437
- }
438
-
439
- export var HtmlReporter: HtmlReporter;
440
- export var HtmlSpecFilter: HtmlSpecFilter;
441
- export var DEFAULT_TIMEOUT_INTERVAL: number;
442
- }