How to use webiny-model - 10 common examples

To help you get started, we’ve selected a few webiny-model examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github webiny / webiny-js / packages / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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(
github webiny / webiny-js / packages-utils / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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
                );
            }
github webiny / webiny-js / packages / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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
github webiny / webiny-js / packages-utils / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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
                    );
                }
github webiny / webiny-js / packages / webiny-entity / src / entityAttributes / entitiesAttribute.js View on Github external
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));
    }
github webiny / webiny-js / packages / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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
github webiny / webiny-js / packages-utils / webiny-entity / src / entityAttributes / entitiesAttribute.js View on Github external
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));
    }
github webiny / webiny-js / packages-utils / webiny-entity / src / entityAttributes / entityAttribute.js View on Github external
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
github webiny / webiny-js / packages / webiny-api / src / attributes / bufferAttribute.js View on Github external
setValue(value) {
        if (typeof value === "string") {
            value = Buffer.from(value.split(",").pop(), this.encoding);
        }
        return Attribute.prototype.setValue.call(this, value);
    }
github webiny / webiny-js / packages / webiny-entity / src / entityAttributes / entitiesAttribute.js View on Github external
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);
        }
    }