Closed tobad357 closed 1 year ago
I have the same issue, but I'm just running npx typewriter init
, javascript and node.js as language options.
I got the same when running in dev mode, however if I changed the function below in the generated code https://github.com/segmentio/typewriter/blob/83b543bcdf2c464272b59b239f7117f4913a5e05/src/languages/templates/typescript/analytics-js.hbs#L195
from
function validateAgainstSchema(
message: TrackMessage<Record<string, any>>,
schema: object
) {
const ajv = new Ajv({ allErrors: true, verbose: true });
if (!ajv.validate(schema, message.properties) && ajv.errors) {
onViolation(message, ajv.errors);
}
}
to
function validateAgainstSchema(
message: TrackMessage<Record<string, any>>,
schema: object
) {
const ajv = new Ajv({ allErrors: true, verbose: true });
ajv.addKeyword("labels");
if (!ajv.validate(schema, message.properties) && ajv.errors) {
onViolation(message, ajv.errors);
}
}
Then it works
Thanks for reporting this. The last update came in with the validation that should've been disabled for production. Pushing a fix right now.
The latest release should fix these errors: https://github.com/segmentio/typewriter/releases/tag/v8.0.10
Ping me if this still happens to you!
@oscb still happens to me
here is an example event that's been generated
export function contactAdded(
message: TrackMessage<ContactAdded>,
callback?: Callback
): void {
const event = withTypewriterContext({
...message,
event: 'Contact Added',
properties: {
...message.properties,
},
});
const schema = {"$id":"Contact Added","$schema":"http://json-schema.org/draft-07/schema#","description":"Player adds a contact","labels":{"area":"account","type":"contacts"},"properties":{"id":{"$id":"/properties/id","description":"the user contact id","type":"string"},"type":{"$id":"/properties/type","description":"The type of contact (MOBILE,EMAIL,ADDRESS)","enum":["ADDRESS","EMAIL","MOBILE"],"type":"string"}},"type":"object"};
validateAgainstSchema(event, schema);
const a = analytics()
if (a) {
a.track(event,callback);
} else {
throw missingAnalyticsNodeError
}
}
and the validateAgainstSchema function
function validateAgainstSchema(
message: TrackMessage<Record<string, any>>,
schema: object
) {
const ajv = new Ajv({ allErrors: true, verbose: true })
if (!ajv.validate(schema, message.properties) && ajv.errors) {
onViolation(message, ajv.errors)
}
}
The error is
Error: strict mode: unknown keyword: "labels"
+1 this
Validation error when generating plan
Step 1 Setup a plan in Segment UI and add labels to the events
Step 2 Run
Step 3 Receive error
Error: strict mode: unknown keyword: "labels"
which seems to come from AJV Full Error BelowStep 4 Run
npm typewriter
and generation seems to work fine