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

Comma in serialization result #2001

Closed
piliugin-anton opened this issue Jun 5, 2022 · 4 comments
Closed

Comma in serialization result #2001

piliugin-anton opened this issue Jun 5, 2022 · 4 comments
Milestone

Comments

@piliugin-anton
Copy link
Contributor

piliugin-anton commented Jun 5, 2022

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

Version: 8.11.0, JTD

Ajv options object

{
  coerceTypes: 'array',
  useDefaults: true,
  removeAdditional: true,
  uriResolver: fastUri,
  allErrors: false
}

JSON Schema

{
  "optionalProperties": {
    "test": {
      "type": "boolean"
    },
    "error": {
      "type": "string"
    }
  },
  "additionalProperties": false
}

Sample data

{ "error": "Something went wrong" }

Your code

const AjvJTD = require('ajv/dist/jtd')

const ajv = new AjvJTD({
  coerceTypes: 'array',
  useDefaults: true,
  removeAdditional: true,
  uriResolver: fastUri,
  allErrors: false
})
const serialize = ajv.compileSerializer(schema)
const result = serialize(object)

Serialized data (NOTICE COMMA)

{,"error":"Something went wrong"}

What results did you expect?

{"error":"Something went wrong"}

Are you going to resolve the issue?
Yes, look at #2028

@piliugin-anton
Copy link
Contributor Author

I'm trying to achieve the result when serialized object could have some data on success or error

@craggy2593
Copy link

Also experiencing the same issue. With a schema of only optional properties, when the first optional property in the schema is missing from the object, the serialized output has a comma at the start, causing JSON validation issues downstream.

@epoberezkin
Copy link
Member

That does look like a bug - the fix should be simple - PR is welcome, otherwise I’ll fix some time next month.

@piliugin-anton
Copy link
Contributor Author

@epoberezkin Please have a look at #2028

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