Skip to content

Commit

Permalink
Comment PropertyEngine.ts;
Browse files Browse the repository at this point in the history
Rename;
  • Loading branch information
Richard Haddad committed Aug 20, 2019
1 parent a04747e commit 27a1676
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 5 deletions.
38 changes: 34 additions & 4 deletions src/engine/PropertyEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { ClassLike } from '../types/ClassTypes';
import { JSONSchema7 } from 'json-schema';

export default class PropertyEngine {
private static getJSONType(typeTS: ClassLike): JSONSchema7 {
/**
* From a class, return a JSONSchema which may contain only the 'type' associated to this class.
*
* @param typeTS
*/
private static getJSONSchemaType(typeTS: ClassLike): JSONSchema7 {
switch (typeTS) {
case Array:
return {
Expand All @@ -30,12 +35,19 @@ export default class PropertyEngine {
}
}

static getJSONPropertyEntity<J extends JSONEntity<any, any>>(
/**
* Return a JSON Schema for a property.
*
* @param reflectEntity partial schema get by reflection
* @param paramEntity partial schema given in param
* @param typeTS reflected type of the property
*/
static getJSONPropertySchema<J extends JSONEntity<any, any>>(
reflectEntity: JSONSchema7,
paramEntity: JSONSchema7,
typeTS: ClassLike
): JSONSchema7 {
const typeEntity = PropertyEngine.getJSONType(typeTS) as J;
const typeEntity = PropertyEngine.getJSONSchemaType(typeTS) as J;

return {
...typeEntity,
Expand All @@ -44,19 +56,37 @@ export default class PropertyEngine {
};
}

/**
* Return by reflection JSON properties (aka JSONSchema.properties) of the JSONSchema.
*
* @param prototype prototype of the JSONSchema class
*/
static getReflectProperties(
prototype: ClassLike['prototype']
): object & Exclude<JSONSchema7['properties'], undefined> {
return Reflect.getMetadata(REFLECT_KEY.JSON_PROPERTY, prototype) || {};
}

/**
* Define by reflection JSON properties (aka JSONSchema.properties) of the JSONSchema.
*
* @param prototype prototype of the JSONSchema class
* @param properties JSONSchema.properties of the JSONSchema
*/
private static setReflectProperties(
prototype: ClassLike['prototype'],
properties: JSONSchema7['properties']
): void {
Reflect.defineMetadata(REFLECT_KEY.JSON_PROPERTY, properties, prototype);
}

/**
* Define JSON schema of the given property.
*
* @param prototype prototype of the JSONSchema class
* @param key property key of the JSONSchema class
* @param value partial schema given in param
*/
static defineReflectProperties<C extends ClassLike>(
prototype: C['prototype'],
key: keyof C['prototype'] & string,
Expand All @@ -69,7 +99,7 @@ export default class PropertyEngine {

const typeSchema: ClassLike = Reflect.getMetadata(REFLECT_KEY.TYPE, prototype, key);

const fullSchema: JSONSchema7 = PropertyEngine.getJSONPropertyEntity<JSONEntityAny>(
const fullSchema: JSONSchema7 = PropertyEngine.getJSONPropertySchema<JSONEntityAny>(
reflectSchema,
value,
typeSchema
Expand Down
2 changes: 1 addition & 1 deletion src/engine/SchemaEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export default class SchemaEngine {
paramEntity: JSONSchema7,
name: string
): JSONSchema7 {
return PropertyEngine.getJSONPropertyEntity<JSONEntityObject>(
return PropertyEngine.getJSONPropertySchema<JSONEntityObject>(
reflectEntity,
paramEntity,
Object
Expand Down

0 comments on commit 27a1676

Please sign in to comment.