|
|
@@ -2,21 +2,9 @@ |
|
|
|
import { arrayProp, prop, getModelForClass } from '@typegoose/typegoose'; |
|
|
|
|
|
|
|
/** |
|
|
|
* @class A interface for the Product schema. The model is derived from the properties and their decorators. |
|
|
|
* The class also has a lot of static methods to validate itself and its properties. |
|
|
|
* @class Base class for models with helper functions to validate JSON inputs |
|
|
|
*/ |
|
|
|
export class Product { |
|
|
|
@prop({ required: true, unique: true, dropDupes: true }) |
|
|
|
public identifier!: string; |
|
|
|
@prop({ required: true }) |
|
|
|
public name!: string; |
|
|
|
@prop({ required: true }) |
|
|
|
public price!: number; |
|
|
|
@arrayProp({ items: String }) |
|
|
|
public sizes?: string[]; |
|
|
|
@prop({ required: true }) |
|
|
|
public category!: string; |
|
|
|
|
|
|
|
class BaseModel { |
|
|
|
/** |
|
|
|
* Validates sizes array, making sure it either is undefined, or defined. All items have to be strings. |
|
|
|
* @param possibleArray - Object to run tests against. Can be anything. |
|
|
@@ -79,12 +67,31 @@ export class Product { |
|
|
|
// For simplicity, we will reject strings that can be converted to numbers for now |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* @class A interface for the Product schema. The model is derived from the properties and their decorators. |
|
|
|
* The class inherits helper methods to validate itself and its properties. |
|
|
|
*/ |
|
|
|
export class Product extends BaseModel { |
|
|
|
@prop({ required: true, unique: true, dropDupes: true }) |
|
|
|
public identifier!: string; |
|
|
|
@prop({ required: true }) |
|
|
|
public name!: string; |
|
|
|
@prop({ required: true }) |
|
|
|
public price!: number; |
|
|
|
@arrayProp({ items: String }) |
|
|
|
public sizes?: string[]; |
|
|
|
@prop({ required: true }) |
|
|
|
public category!: string; |
|
|
|
|
|
|
|
/** |
|
|
|
* Validates object if it can be cast to product interface. |
|
|
|
* Checks if all required fields are provided and that the data meets the guidelines. |
|
|
|
* If extra fields given, we will just ignore them. Possible issue here because we are failing quietly. |
|
|
|
* @param jsonInput - Object to be casted to Product interface |
|
|
|
* @throws error if a received part of data is not valid and cannot be parsed |
|
|
|
* @returns Product interface with valid fields and data |
|
|
|
*/ |
|
|
|
public static fromJson(jsonInput: any): Product { |