Skip to content

Validate a Cloud Event on creation and make it read only with a method to augment it #234

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

Merged
merged 1 commit into from
Jul 13, 2020

Conversation

lholmquist
Copy link
Contributor

@lholmquist lholmquist commented Jun 30, 2020

  • Validate the Cloud Event in the Constructor
  • Freeze the Object that is created
  • Update/Add extensions after the fact
  • Modify other properties if needed
  • Update Tests

During the creation of a CloudEvent, the validate method is now called. The object is also frozen to make it read-only.

A new method cloneWith has been added which can be used to add/update attributes such as extensions and will return a new Cloud Event Object

fixes #233

related #29

Signed-off-by: Lucas Holmquist lholmqui@redhat.com

@lholmquist
Copy link
Contributor Author

lholmquist commented Jul 1, 2020

I just added Initial support for validating with the Proxy. Some tests are still failing, but I would like to talk about this Proxy handler:

return new Proxy(this, {
      // obj is the instance of the CloudEvent
      // prop is the property being set
      // value is the value on the right side of the equal sign
      set: function (obj, prop: string, value) {
        // Make a copy of the incoming Object, need to have all it's properties and such
        const updateObj = Object.create(obj);
        // Update it with the new value
        updateObj[prop] = value;

        // Validate the object
        obj.validate(updateObj);

        // If we succeed, then Update the real object
        // Set the new value normally
        obj[prop] = value;

        return true;
      },
    });

At first the handler was simpler and looked like this :

set: function (obj, prop, vale) {
  obj[prop] = value;
  return this.validate()
}

edit: i also just realized that we have set methods for data and time that do somthing special, these would most likely need to be moved into the proxy handler

The only problem with this, is if a validation fails, the value passed in was still being assigned. This was causing some tests to fail because we are re-using the same CloudEvent Object across multiple tests. In a real application it might not be an issue, since if an invalid value is assigned, an error is thrown. So not sure if this is overkill and we should re-write the tests or if we should try to validate before assigning. Hopefully what i just rambled out makes sense :)

Also the reason i'm using Object.create and not ... is because ... strips off the set and get methods of time and data and we need those.

@lholmquist
Copy link
Contributor Author

Going in a different direction. Not using Proxy any more but using the "cloneWith" method instead.

I'll need to update the tests

@lholmquist lholmquist force-pushed the 233-freeze-and-add branch 3 times, most recently from 615ab02 to 3116ffc Compare July 6, 2020 22:26
@lholmquist lholmquist marked this pull request as ready for review July 6, 2020 22:27
@lholmquist
Copy link
Contributor Author

For this PR i had originally thought to add the validation in #228 and #229, but i think those should be its own PR and this one should just focus on calling validate during object creation as well as making the object read-only.

I have another branch with the extension name validation ready to go once this gets in

@lholmquist lholmquist changed the title WIP: Augment a Read-only Cloud Event Validate a Cloud Event on creation and make it read only with a method to augment it Jul 6, 2020
@lholmquist
Copy link
Contributor Author

Ping @cloudevents/sdk-javascript-maintainers PTAL

@lholmquist lholmquist requested review from a team, lance, grant, fabiojose and helio-frota and removed request for a team July 6, 2020 22:31
@lholmquist lholmquist added the version/3.x Issues related to the 3.0 release of this library label Jul 6, 2020
@lholmquist lholmquist force-pushed the 233-freeze-and-add branch from 8d32ac0 to c20a10f Compare July 6, 2020 22:47
@@ -35,8 +35,7 @@ const cloudevent = new CloudEvent({
dataschema,
data,
});
cloudevent[ext1Name] = ext1Value;
cloudevent[ext2Name] = ext2Value;
cloudevent = cloudevent.cloneWith({ [ext1Name]: ext1Value, [ext2Name]: ext2Value });
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why can't we just add this to the original CloudEvent?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably could. Looks like a leftover of when you couldn't add extensions during construction

cloudevent.datacontentencoding = Constants.ENCODING_BASE64;
cloudevent = cloudevent.cloneWith({
datacontentencoding: Constants.ENCODING_BASE64,
data: "SSB3YXMgZnVubnkg8J+Ygg==",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here, can you post the original string, then btoa it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are other tests that already have the string encoded, so the precedent was already set. If this is a blocker, then we can create another PR to update the other tests as well

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a TODO if other PRs are doing this?
It's still not ideal as I can't read or understand SSB3...

Copy link
Member

@lance lance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM - nice changes to the test files using cloneWith instead of mucking around with a single event, changing it as needed for each test.

@lholmquist lholmquist force-pushed the 233-freeze-and-add branch from 433ecc3 to df8a2fb Compare July 7, 2020 13:56
@lholmquist lholmquist requested a review from grant July 8, 2020 17:32
@lholmquist
Copy link
Contributor Author

@cloudevents/sdk-javascript-maintainers how do we feel about merging this?

Copy link
Member

@lance lance left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall LGTM. I've noted a few places where the tests got looser (just testing expect.to.throw() instead of the type of Error and message). I think these should retain the original intent.These and the others I didn't call out should probably be changed to the stricter throw(ValidationError, "invalid payload").

@lholmquist lholmquist force-pushed the 233-freeze-and-add branch from df8a2fb to 783095e Compare July 13, 2020 14:12
@lholmquist
Copy link
Contributor Author

@lance i updated the tests to make sure they are throwing a TypeError, which is the error that will be thrown when trying to delete a property on a frozen object.

@lholmquist
Copy link
Contributor Author

@grant are you ok with this PR. Your comments were related to how some of the tests were written, which i've tried to give an explanation for. If this is still a blocker, maybe we could address those in a separate PR?

@grant
Copy link
Member

grant commented Jul 13, 2020

Looks good to me. Thanks for the PR!

@lholmquist lholmquist force-pushed the 233-freeze-and-add branch from 3daed0b to 5ec847e Compare July 13, 2020 19:10
…nt itself.

BREAKING CHANGE:

* This change makes the CloudEvent Read-only and validates the input during object creation.

* To augment an already created CloudEvent object, we have added a `cloneWith` method that takes attributes to add/update.

Signed-off-by: Lucas Holmquist <lholmqui@redhat.com>
@lholmquist lholmquist force-pushed the 233-freeze-and-add branch from 5ec847e to d0099e2 Compare July 13, 2020 19:13
@lholmquist lholmquist merged commit c7a8477 into cloudevents:master Jul 13, 2020
This was referenced Jul 21, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
version/3.x Issues related to the 3.0 release of this library
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A CloudEvent should be readonly but provide a way to augment itself
4 participants