We have MasterData
entities annotated with Firefly Semantics Validator annotations:
/**
* The Master Data
*/
export class MasterData {
gid?: string;
id?: string;
@IsString()
@IsDefined()
public sku!: string;
@IsString()
@IsDefined()
public supplier!: string;
@IsString()
@IsDefined()
public location!: string;
@IsNumber()
@IsDefined()
public safetyStock: number = 0;
@IsNumber()
@IsDefined()
public leadTimeDays!: number;
public orderSize: number = 1;
constructor(md?: any) {
if (md) {
Object.assign(this, md);
}
}
}
We want to batch validate the instances, and for each property that is invalid we will produce an instance of the following EntityError
interface:
/**
* Interface for Entity error reporting.
*/
export interface EntityError {
id: string;
property: string;
value: string;
type: string;
message: string;
}
Approach
In order to demo the approach we will create 2 MasterData
objects:
//=============================================
// Missing leadTimeDays
//=============================================
const md1: any = {
sku: 'sk2',
supplier: 's2',
location: 'l2',
safetyStock: 10,
orderSize: 4,
};
//=============================================
// Missing orderSize
//=============================================
const md2: any = {
sku: 'sk2',
supplier: 's2',
location: 'l2',
safetyStock: 10,
leadTimeDays: 15,
};
The first is missing the leadTimeDays
property and the second is missing the orderSize
property.
We then create 2 MasterData
instances using the properties and put them in an array:
const MD1 = new MasterData(md1);
const MD2 = new MasterData(md2);
const MDS = [MD1, MD2];
We validate the error using the Firefly Semantics Validator validateEntities
function:
const EES: EntityError[] = validateEntities(MDS, 'sku');
Note that the second argument sku
is the name of the property that maps to the id
property of the EntityError
interface.
That produces 2 EntityError
instances. One for each property that is invalid:
[
{
"id":"sk2",
"property":"leadTimeDays",
"message":"The value contained by leadTimeDays, undefined, should not be null or undefined",
"value": undefined,
"type":"MasterData"
},
{
"id":"sk2",
"property":"orderSize",
"message":"The value contained by orderSize, undefined, should not be null or undefined",
"value": undefined,
"type":"MasterData"
}
]