OptimumCode/json-schema-validator

Bug: element is not a object

Closed this issue · 4 comments

P72B commented

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Setup: KMP project that includes json-schema-validator lib.
JsonSchema.validate doesn't work on JS lib export. The kotlin unit test inside the kmp project works and the schema gets validated correctly. But when I take the generated JS lib output (gradle jsRun) from the kmp then I get an errror during NodeJS runtime schema validation attempt with the message "element is not a object".

Expected Behavior

It should be validating same json schema on kotlin as well as on nodejs platform.

JSON schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/product.schema.json",
  "title": "Product",
  "description": "A product from Acme's catalog",
  "type": "object",
  "properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    },
    "productName": {
      "description": "Name of the product",
      "type": "string"
    },
    "price": {
      "description": "The price of the product",
      "type": "number",
      "exclusiveMinimum": 0
    }
  },
  "required": [ "productId", "productName", "price" ]
}

Library version

0.0.15

Anything else?

kmp code kmp unit test nodejs code implementing JS kmp lib Error message

Hi, @P72B. Thank you for reporting this issue.

Could you share a minimum project so I can reproduce the issue locally?
And what does req.body return in JS? Are you sure it returns JsonElement? I have not worked with JS for a long time, probably, you need first to deserialize the request payload using kotlin-serialization library in JS and only then pass it to the validation function

P72B commented

Hi @OptimumCode thank you for that incredible fast answer. You where right it's on the JS side. I had to JSON.stringify(reg.body) of course. I was so sure it's a valid JsonElement ^^. So there is no bug. It's just me beeing blind.

I appreciate your work on this great lib. Thank you.

P72B commented

Invalid bug report

No worries, was happy to help) And I am glad that somebody uses the library)