property mapping validation #48
This commit is contained in:
parent
3e79f8ad77
commit
68ec5135cc
6 changed files with 146 additions and 23 deletions
|
|
@ -1,4 +1,4 @@
|
|||
import {containsOnlyLettersAndUnderscores} from '../utils/Utils';
|
||||
import {containsOnlyLettersAndUnderscores, PropertyMappingNameConflictError, PropertyMappingValidationError} from '../utils/Utils';
|
||||
import {MediaType} from '../utils/MediaType';
|
||||
|
||||
export enum PropertyMappingOption {
|
||||
|
|
@ -9,9 +9,73 @@ export enum PropertyMappingOption {
|
|||
|
||||
export const propertyMappingOptions = [PropertyMappingOption.Default, PropertyMappingOption.Map, PropertyMappingOption.Remove];
|
||||
|
||||
export interface PropertyMappingModel {
|
||||
type: MediaType,
|
||||
properties: PropertyMapping[],
|
||||
export class PropertyMappingModel {
|
||||
type: MediaType;
|
||||
properties: PropertyMapping[];
|
||||
|
||||
constructor(type: MediaType, properties?: PropertyMapping[]) {
|
||||
this.type = type;
|
||||
this.properties = properties ?? [];
|
||||
}
|
||||
|
||||
validate(): { res: boolean, err?: Error } {
|
||||
// check properties
|
||||
for (const property of this.properties) {
|
||||
const propertyValidation = property.validate();
|
||||
if (!propertyValidation.res) {
|
||||
return {
|
||||
res: false,
|
||||
err: propertyValidation.err,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
// check for name collisions
|
||||
for (const property of this.getMappedProperties()) {
|
||||
const propertiesWithSameTarget = this.getMappedProperties().filter(x => x.newProperty === property.newProperty);
|
||||
if (propertiesWithSameTarget.length === 0) {
|
||||
// if we get there, then something in this code is wrong
|
||||
} else if (propertiesWithSameTarget.length === 1) {
|
||||
// all good
|
||||
} else {
|
||||
// two or more properties are mapped to the same property
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingNameConflictError(`Multiple remapped properties (${propertiesWithSameTarget.map(x => x.toString()).toString()}) may not share the same name.`),
|
||||
};
|
||||
}
|
||||
}
|
||||
// remapped properties may not have the same name as any original property
|
||||
for (const property of this.getMappedProperties()) {
|
||||
const propertiesWithSameTarget = this.properties.filter(x => x.newProperty === property.property);
|
||||
if (propertiesWithSameTarget.length === 0) {
|
||||
// all good
|
||||
} else {
|
||||
// a mapped property shares the same name with an original property
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingNameConflictError(`Remapped property (${property}) may not share it's new name with an existing property.`),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
res: true,
|
||||
};
|
||||
}
|
||||
|
||||
getMappedProperties() {
|
||||
return this.properties.filter(x => x.mapping === PropertyMappingOption.Map);
|
||||
}
|
||||
|
||||
copy(): PropertyMappingModel {
|
||||
const copy = new PropertyMappingModel(this.type);
|
||||
for (const property of this.properties) {
|
||||
const propertyCopy = new PropertyMapping(property.property, property.newProperty, property.mapping, property.locked);
|
||||
copy.properties.push(propertyCopy);
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
}
|
||||
|
||||
export class PropertyMapping {
|
||||
|
|
@ -49,16 +113,47 @@ export class PropertyMapping {
|
|||
*/
|
||||
}
|
||||
|
||||
validate(): string {
|
||||
validate(): { res: boolean, err?: Error } {
|
||||
// locked property may only be default
|
||||
if (this.locked) {
|
||||
if (this.mapping === PropertyMappingOption.Remove) {
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingValidationError(`Error in property mapping "${this.toString()}": locked property may not be removed.`),
|
||||
};
|
||||
}
|
||||
if (this.mapping === PropertyMappingOption.Map) {
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingValidationError(`Error in property mapping "${this.toString()}": locked property may not be remapped.`),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
if (this.mapping === PropertyMappingOption.Default) {
|
||||
return {res: true};
|
||||
}
|
||||
if (this.mapping === PropertyMappingOption.Remove) {
|
||||
return {res: true};
|
||||
}
|
||||
|
||||
if (!this.property || !containsOnlyLettersAndUnderscores(this.property)) {
|
||||
return `Error in conversion rule "${this.toString()}": property may not be empty and only contain letters and underscores.`;
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingValidationError(`Error in property mapping "${this.toString()}": property may not be empty and only contain letters and underscores.`),
|
||||
};
|
||||
}
|
||||
|
||||
if (!this.newProperty || !containsOnlyLettersAndUnderscores(this.newProperty)) {
|
||||
return `Error in conversion rule "${this.toString()}": new property may not be empty and only contain letters and underscores.`;
|
||||
return {
|
||||
res: false,
|
||||
err: new PropertyMappingValidationError(`Error in property mapping "${this.toString()}": new property may not be empty and only contain letters and underscores.`),
|
||||
};
|
||||
}
|
||||
|
||||
return '';
|
||||
return {
|
||||
res: true,
|
||||
};
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue