Browse Source

Moved helper methods outside of Product and into generic Base class

main
Roxie Gibson 4 years ago
parent
commit
f526f24341
No known key found for this signature in database
2 changed files with 23 additions and 15 deletions
  1. +21
    -14
      src/db/models.ts
  2. +2
    -1
      tslint.json

+ 21
- 14
src/db/models.ts View File

@@ -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 {

+ 2
- 1
tslint.json View File

@@ -6,7 +6,8 @@
"jsRules": {},
"rules": {
"no-console": false,
"semicolon": true
"semicolon": true,
"max-classes-per-file": false
},
"rulesDirectory": []
}

Loading…
Cancel
Save