Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
if (valueValidation && this.hasMultipleEntityClasses()) {
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(
async validate() {
// If attribute is dirty, has validators or loading is in progress, wait until loaded.
if (this.value.isDirty() || this.hasValidators() || this.value.isLoading()) {
await this.value.load();
}
if (!this.value.isLoaded()) {
return;
}
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
if (valueValidation && this.hasMultipleEntityClasses()) {
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
);
}
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
);
}
async validate() {
const valueInstance = ((this.value: any): EntitiesAttributeValue);
// If attribute has validators or loading is in progress, wait until loaded.
const mustValidate =
valueInstance.isDirty() || this.hasValidators() || valueInstance.isLoading();
if (!mustValidate) {
return;
}
await valueInstance.load();
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
valueValidation && (await this.validateType(value));
await this.validateAttribute(value);
valueValidation && (await this.validateValue(value));
}
async validate() {
// If attribute is dirty, has validators or loading is in progress, wait until loaded.
const valueInstance = ((this.value: any): EntityAttributeValue);
if (valueInstance.isDirty() || this.hasValidators() || valueInstance.isLoading()) {
await valueInstance.load();
}
if (!valueInstance.isLoaded()) {
return;
}
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
if (valueValidation && this.hasMultipleEntityClasses()) {
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
async validate() {
// If attribute has validators or loading is in progress, wait until loaded.
const mustValidate = this.value.isDirty() || this.hasValidators() || this.value.isLoading();
if (!mustValidate) {
return;
}
await this.value.load();
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
valueValidation && (await this.validateType(value));
await this.validateAttribute(value);
valueValidation && (await this.validateValue(value));
}
async validate() {
// If attribute is dirty, has validators or loading is in progress, wait until loaded.
if (this.value.isDirty() || this.hasValidators() || this.value.isLoading()) {
await this.value.load();
}
if (!this.value.isLoaded()) {
return;
}
const value = await this.getValidationValue();
const valueValidation = !Attribute.isEmptyValue(value);
if (valueValidation && this.hasMultipleEntityClasses()) {
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
setValue(value) {
if (typeof value === "string") {
value = Buffer.from(value.split(",").pop(), this.encoding);
}
return Attribute.prototype.setValue.call(this, value);
}
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);
}
}