Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attribute validation not invoked when using data update method #162

Closed
dangreaves opened this issue Oct 20, 2022 · 2 comments · Fixed by #171
Closed

Attribute validation not invoked when using data update method #162

dangreaves opened this issue Oct 20, 2022 · 2 comments · Fixed by #171

Comments

@dangreaves
Copy link

Describe the bug
When updating attributes via the data() update method, attribute validations are not invoked.

ElectroDB Version
2.1.2

Entity/Service Definitions

Using this entity, note that there is a validate method on the prop1 attribute, which will fail if the attribute does not contain the substring foo.

{
    model: {
        entity: "my_entity",
        service: "my_service",
        version: "my_version"
    },
    attributes: {
        prop1: {
            type: "string",
            validate: (value) => !value.includes("foo"),
        },
        prop2: {
            type: "string"
        }
    },
    indexes: {
        record: {
            pk: {
                field: "pk",
                composite: ["prop1"],
            },
            sk: {
                field: "sk",
                composite: ["prop2"],
            },
        }
    } 
}

If we do this, then the validation fails as expected, and we get an error.

MyEntity.update({ entityID }).set({ prop1: "bar" });

However, if we do this instead, then the validation does not fail. In fact, the validate callback is not invoked at all.

MyEntity.update({ entityID }).data((attributes, operations) => {
  operations.set(attributes.prop1, "bar");
});

Expected behavior
Attribute validations should be invoked when using the update methods inside the data method.

Additional context
I understand that the underlying logic of the data method is probably different from the standard set, update etc, but the data method is really helpful for performing conditional operations. For example, I might call the set or remove operation based on the value of an attribute. This behavior is not possible using the default method chaining technique.

@tywalch
Copy link
Owner

tywalch commented Oct 21, 2022

Thank you! I will look into this this weekend!

@tywalch
Copy link
Owner

tywalch commented Nov 2, 2022

As a follow up, I'm hoping to get a change for this merged soon 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants