Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
if (!this.options.classIdAttribute) {
throw new ModelError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but does not have "classIdAttribute" option defined.`,
ModelError.INVALID_ATTRIBUTE
);
}
let classIdAttribute = this.getClassIdAttribute();
if (!classIdAttribute) {
throw new ModelError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but classId attribute is missing.`,
ModelError.INVALID_ATTRIBUTE
);
}
// We only do class validation if list of classes has been provided. Otherwise, we don't do the check.
// This is because in certain cases, a list of classes cannot be defined, and in other words, any
// class of entity can be assigned. One example is the File entity, which has an "ref" attribute, which
// can actually link to any type of entity.
if (!this.canAcceptAnyEntityClass()) {
if (!this.getEntityClass()) {
const heldValue = await classIdAttribute.getValue();
if (!(typeof heldValue === "string")) {
throw new EntityError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but it was not found (classId attribute holds invalid non-string value).`,
ModelError.INVALID_ATTRIBUTE
if (!this.options.classIdAttribute) {
throw new ModelError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but does not have "classIdAttribute" option defined.`,
ModelError.INVALID_ATTRIBUTE
);
}
let classIdAttribute = this.getClassIdAttribute();
if (!classIdAttribute) {
throw new ModelError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but classId attribute is missing.`,
ModelError.INVALID_ATTRIBUTE
);
}
// We only do class validation if list of classes has been provided. Otherwise, we don't do the check.
// This is because in certain cases, a list of classes cannot be defined, and in other words, any
// class of entity can be assigned. One example is the File entity, which has an "ref" attribute, which
// can actually link to any type of entity.
if (!this.canAcceptAnyEntityClass()) {
if (!this.getEntityClass()) {
throw new EntityError(
`Entity attribute "${
this.name
}" accepts multiple Entity classes but it was not found (classId attribute holds value "${classIdAttribute.getValue()}").`,
ModelError.INVALID_ATTRIBUTE
);
}
continue;
}
try {
await currentEntity.validate();
} catch (e) {
errors.push({
code: e.code,
data: { index: i, ...e.data },
message: e.message
});
}
}
if (!_.isEmpty(errors)) {
throw new ModelError("Validation failed.", ModelError.INVALID_ATTRIBUTE, errors);
}
}
continue;
}
try {
await currentEntity.validate();
} catch (e) {
errors.push({
code: e.code,
data: { index: i, ...e.data },
message: e.message
});
}
}
if (!_.isEmpty(errors)) {
throw new ModelError("Validation failed.", ModelError.INVALID_ATTRIBUTE, {
items: errors
});
}
}
async validateValue(value: mixed) {
const errors = [];
const correctClass = this.getUsingClass() || this.getEntitiesClass();
if (!Array.isArray(value)) {
return;
}
for (let i = 0; i < value.length; i++) {
const currentEntity = value[i];
if (!(Entity.isInstanceOf(currentEntity, correctClass))) {
errors.push({
code: ModelError.INVALID_ATTRIBUTE,
data: {
index: i
},
message: `Validation failed, item at index ${i} not an instance of correct Entity class.`
});
continue;
}
try {
await currentEntity.validate();
} catch (e) {
errors.push({
code: e.code,
data: { index: i, ...e.data },
message: e.message
});
async validateValue(value: mixed) {
const errors = [];
const correctClass = this.getUsingClass() || this.getEntitiesClass();
if (!Array.isArray(value)) {
return;
}
for (let i = 0; i < value.length; i++) {
const currentEntity = value[i];
if (!(currentEntity instanceof correctClass)) {
errors.push({
code: ModelError.INVALID_ATTRIBUTE,
data: {
index: i
},
message: `Validation failed, item at index ${i} not an instance of correct Entity class.`
});
continue;
}
try {
await currentEntity.validate();
} catch (e) {
errors.push({
code: e.code,
data: { index: i, ...e.data },
message: e.message
});