segmentio / typewriter

Type safety + intellisense for your Segment analytics
https://segment.com/docs/protocols/typewriter/
MIT License
227 stars 53 forks source link

Bug: Error when validateAgainstSchema is run #281

Closed cjhensen closed 1 year ago

cjhensen commented 1 year ago

Thanks for taking the time to contribute to Typewriter!

Hey @oscb! Thanks for fixing the usage of validateAgainstSchema (this issue)!

Running on localhost with my analytics library generated in "development" mode, I see errors being thrown on schema validation: image

Looking at the schema defined in each tracking function, such as:

    const schema = {"$id":"Thing viewed","$schema":"http://json-schema.org/draft-07/schema#","description":"A thing notification was viewed by the member","properties":{"component":{"$id":"/properties/component","description":"","type":"string"},"name":{"$id":"/properties/name","description":"e.g. \"Contact us button\"","type":"string"},"pathName":{"$id":"/properties/pathName","description":"","type":"string"},"title":{"$id":"/properties/title","description":"title in the Thing","type":"string"},"url":{"$id":"/properties/url","description":"","type":"string"}},"required":["component","name","pathName","url","title"],"type":"object"};

It's unclear what the data.$id is referring to in the error. Is it the "$id": "Thing viewed"? Furthermore, my event properties in the code match those define in the tracking plan, so not sure how to resolve this issue.

Given the errors, I wanted to disable runtime validation, so I set runtime-typecheck to false in typewriter.yml, however the validateAgainstSchema function is still called for each tracking function after re-generating the client.

oscb commented 1 year ago

Hey @cjhensen sorry for the long wait, I was out and couldn't get to this until today. So the validations actually are the difference between development and production mode. If you make a build for production (build --mode prod) it will remove them

The problem is on our end, the schema id shouldn't have spaces: "$id": "Thing viewed" so either our servers or Typewriter need to sanitize those (pushing a fix for Typewriter for now, until we get that fixed on the server). Sorry for the inconvenience!

richessler commented 1 year ago

@oscb thanks for the answer, I'm not seeing that as the issue on my end. I'm running into the exact same issue as @cjhensen but my analytics file wasn't generated with spaces. Any other thoughts?