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

JTD compileSerializer and compileParser failure with optionalProperties #2181

Closed
jkav77 opened this issue Dec 17, 2022 · 1 comment
Closed

Comments

@jkav77
Copy link

jkav77 commented Dec 17, 2022

What version of Ajv are you using? Does the issue happen if you use the latest version?
8.11.2: the latest version

Ajv options object
{}

Your code

import Ajv, { JTDSchemaType } from "ajv/dist/jtd";
const ajv = new Ajv();

interface TestObject {
  first?: string;
  second?: string;
}

const testSchema: JTDSchemaType<TestObject> = {
  optionalProperties: {
    first: { type: "string" },
    second: { type: "string" },
  },
};

const serialize = ajv.compileSerializer(testSchema);
const parse = ajv.compileParser(testSchema);

const obj = { second: "test" };

const serializedObj = serialize(obj);
const parsedObj = parse(serializedObj);

console.log(serializedObj);
console.log(parsedObj);
console.log(parse.message, parse.position);

Output

{,"second":"test"}
undefined
unexpected token , 1

What results did you expect?
The serializer should create json code without the erroneous ,.

{"second":"test"}

Are you going to resolve the issue?
I will look into it, but I haven't found the source of the issue yet.

@jkav77
Copy link
Author

jkav77 commented Dec 17, 2022

Related to #2171, which addresses the additionalProperties instead of optionalProperties.

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

Successfully merging a pull request may close this issue.

2 participants
@jkav77 and others