-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
associations: handling circular, multiple same, undefined class, and …
…not json class; adding tests
- Loading branch information
Richard Haddad
committed
Aug 16, 2019
1 parent
673e5f5
commit a992d0d
Showing
17 changed files
with
259 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { ClassLike } from '../types/ClassTypes'; | ||
|
||
export const Util = { | ||
isClass(o: any): o is ClassLike { | ||
return o && o['name'] && o['prototype']; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ClassLike } from '../types/ClassTypes'; | ||
|
||
const msg = (classStack: ClassLike[]) => | ||
`Circular dependency detected in the JSON Schema associations, it is not yet supported by Tabbouleh. | ||
This is the path followed: ${classStack.map(c => c.name).join(' => ')}`; | ||
|
||
/** | ||
* To throw when a circular dependency is detected. | ||
*/ | ||
export class CircularDependencyError extends Error { | ||
/** | ||
* @param classStack the stack of classes followed | ||
*/ | ||
constructor(...classStack: ClassLike[]) { | ||
super(msg(classStack)); | ||
Error.call(this); | ||
Error.captureStackTrace(this, this.constructor); | ||
Object.setPrototypeOf(this, CircularDependencyError.prototype); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,27 @@ | ||
import { ClassLike } from './ClassTypes'; | ||
import { JSONSchema7 } from 'json-schema'; | ||
|
||
export type ClassFn<C extends ClassLike = ClassLike> = () => C; | ||
|
||
export type Association<C extends ClassLike = ClassLike> = { | ||
className: C['name']; | ||
|
||
// class property key | ||
/** | ||
* class property key | ||
*/ | ||
key: keyof C['prototype'] & string; | ||
|
||
jsonPropertyKey: keyof JSONSchema7 | null; | ||
|
||
// class targeted | ||
target: ClassLike; | ||
/** | ||
* class targeted | ||
*/ | ||
targetFn: ClassFn; | ||
}; | ||
|
||
export type AssociationMap = { | ||
// class name | ||
/** | ||
* [className]: list Association | ||
*/ | ||
[key: string]: Association[]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import Tabbouleh from '../src/engine/Tabbouleh'; | ||
import { OBJECT_SAMPLE_USER, JSONObjectSample } from './samples/JSONObject.sample'; | ||
import { JSONSchema7 } from 'json-schema'; | ||
|
||
const schemaObjectSample: JSONSchema7 = { | ||
type: 'object', | ||
|
||
properties: { | ||
user: { | ||
type: 'object', | ||
...(OBJECT_SAMPLE_USER as any) | ||
} | ||
} | ||
}; | ||
|
||
describe('check JSONObject', () => { | ||
it('should handle JSONObject with JSONEntity given', () => { | ||
expect(Tabbouleh.generateJSONSchema(JSONObjectSample)).toEqual(schemaObjectSample); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { JSONSchema7 } from 'json-schema'; | ||
import { CSample5, CS5_SCHEMA_PROPS } from './samples/CSample5'; | ||
import Tabbouleh from '../src/engine/Tabbouleh'; | ||
import { FOOD_SCHEMA_PROPS } from './genericSample/Food.sample'; | ||
import { CircularAssociationSample } from './associationSamples/CircularAssociation.sample'; | ||
import { CircularDependencyError } from '../src/exception/CircularDependencyError'; | ||
import { UndefinedAssociationSample } from './associationSamples/UndefinedAssociation.sample'; | ||
import { NotAJsonSchemaError } from '../src/exception/NotAJsonSchemaError'; | ||
import { NotJsonSchemaAssociationSample } from './associationSamples/NotJsonSchemaAssociation.sample'; | ||
import { MultipleSameAssociationSample } from './associationSamples/MultipleSameAssociation.sample'; | ||
|
||
const schemaCSample5: JSONSchema7 = { | ||
type: 'object', | ||
|
||
...CS5_SCHEMA_PROPS, | ||
|
||
properties: { | ||
type: { | ||
type: 'string' | ||
}, | ||
|
||
price: { | ||
type: 'number' | ||
}, | ||
|
||
food: { | ||
type: 'object', | ||
|
||
...FOOD_SCHEMA_PROPS, | ||
|
||
properties: { | ||
parsley: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
const schemaMultipleSameAssociation: JSONSchema7 = { | ||
type: 'object', | ||
properties: { | ||
prop: { | ||
type: 'object', | ||
|
||
...FOOD_SCHEMA_PROPS, | ||
|
||
properties: { | ||
parsley: { | ||
type: 'string' | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
describe('Check if related schemas work', () => { | ||
it('should handle nested object', () => { | ||
expect(Tabbouleh.generateJSONSchema(CSample5)).toEqual(schemaCSample5); | ||
}); | ||
|
||
it('should throw a CircularDependencyError on circular association', () => { | ||
expect(() => Tabbouleh.generateJSONSchema(CircularAssociationSample)).toThrow( | ||
CircularDependencyError | ||
); | ||
}); | ||
|
||
it('should throw a NotAJsonSchemaError on undefined association', () => { | ||
expect(() => Tabbouleh.generateJSONSchema(UndefinedAssociationSample)).toThrow( | ||
NotAJsonSchemaError | ||
); | ||
}); | ||
|
||
it('should throw a NotAJsonSchemaError on no-json-schema association', () => { | ||
expect(() => Tabbouleh.generateJSONSchema(NotJsonSchemaAssociationSample)).toThrow( | ||
NotAJsonSchemaError | ||
); | ||
}); | ||
|
||
it('should handle multiple same association', () => { | ||
expect(Tabbouleh.generateJSONSchema(MultipleSameAssociationSample)).toEqual( | ||
schemaMultipleSameAssociation | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { JSONSchema } from '../../src/decorators/JSONSchema'; | ||
import { JSONObject } from '../../src/decorators/JSONObject'; | ||
|
||
@JSONSchema | ||
export class CircularAssociationSample { | ||
@JSONObject(() => CircularAssociationSample) | ||
inception: CircularAssociationSample; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { JSONSchema } from '../../src/decorators/JSONSchema'; | ||
import { FoodSample } from '../genericSample/Food.sample'; | ||
import { JSONObject } from '../../src/decorators/JSONObject'; | ||
|
||
@JSONSchema | ||
export class MultipleSameAssociationSample { | ||
@JSONObject(() => FoodSample) | ||
@JSONObject(() => FoodSample) | ||
prop: FoodSample; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { JSONSchema } from '../../src/decorators/JSONSchema'; | ||
import { JSONObject } from '../../src/decorators/JSONObject'; | ||
import { NotJsonSchemaSample } from '../genericSample/NotJsonSchema.sample'; | ||
|
||
@JSONSchema | ||
export class NotJsonSchemaAssociationSample { | ||
@JSONObject(() => NotJsonSchemaSample) | ||
fake: unknown; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { JSONSchema } from '../../src/decorators/JSONSchema'; | ||
import { JSONObject } from '../../src/decorators/JSONObject'; | ||
|
||
@JSONSchema | ||
export class UndefinedAssociationSample { | ||
@JSONObject(() => undefined as any) | ||
fake: unknown; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { JSONSchema } from '../../src/decorators/JSONSchema'; | ||
import { JSONString } from '../../src/decorators/JSONString'; | ||
|
||
export const FOOD_SCHEMA_PROPS = { | ||
title: 'Tabbouleh' | ||
}; | ||
|
||
@JSONSchema<FoodSample>(FOOD_SCHEMA_PROPS) | ||
export class FoodSample { | ||
@JSONString | ||
parsley: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export class NotJsonSchemaSample { | ||
toto: number; | ||
|
||
tata: string; | ||
} |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.