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

Typewriter JSON Schema Validation Error for Date objects #313

Open
ivark16 opened this issue Nov 8, 2023 · 1 comment
Open

Typewriter JSON Schema Validation Error for Date objects #313

ivark16 opened this issue Nov 8, 2023 · 1 comment

Comments

@ivark16
Copy link

ivark16 commented Nov 8, 2023

About

Hello, I'm using typewriter and just encountered this problem where I get a validation error stating that JSON Schema doesn't match the Tracking plan spec in the console.

Environment

$ node --version
v16.10.0
$ yarn --version
1.22.19
$ tsc --version 
Version 4.9.5

Using current version of typewriter: 9.1.0

The problem

In my tracking plan I have a property called destination_date_time which is of type Date time.

Automatically generated typewriter client file has this property listed as destination_date_time?: Date;

The plan.json file has this specific property listed as:

"destination_date_time": {
  "$id": "...",
  "description": "...",
  "format": "date-time",
  "type": "string"
},

And when making this call to Segment I get this warning in my console:

 "type": "Typewriter JSON Schema Validation Error",
  "description": "You made an analytics call (undefined) using Typewriter that doesn't match the Tracking Plan spec.",
  "errors": [
    {
      "keyword": "type",
      "dataPath": ".products[0].destination_date_time",
      "schemaPath": "#/properties/products/items/properties/destination_date_time/type",
      "params": {
        "type": "string"
      },
      "message": "should be string",
      "schema": "string",
      "parentSchema": {
        "$id": "...",
        "description": "....",
        "format": "date-time",
        "type": "string"
      },
      "data": "2023-11-09T10:45:00.000Z"
    },
    .
    . 
    . 

I have tried just giving it new Date() as value to check if it was my data that wasn't of the right type but I still get the same error. And if I try feeding it any other than Date typewriter will give me an error saying it only accepts Date types but then again I get this issue in the console when developing locally. Not sure if this is a known issue or not but wanted to raise this and ask if there is any way around getting this warning 😄

@inazurov
Copy link

I'm facing the same problem. As workaround I have changed the generated client segment.ts implementation in the following way:

import addFormats from 'ajv-formats';
function validateAgainstSchema(
  message: TrackMessage<Record<string, any>>,
  schema: object,
) {
  const ajv = new Ajv({ allErrors: true, verbose: true });
  addFormats(ajv);
  if (!ajv.validate(schema, message.properties) && ajv.errors) {
    onViolation(message, ajv.errors);
  }
}

I have added the formats manually but this will be gone as soon as the client is regenerated again.

I am looking for better solution....

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

No branches or pull requests

2 participants