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

compileParser does not appear to support properties with type field?: Record<string, unknown> via JTDSchemaType. #2418

Open
elhedran opened this issue Apr 18, 2024 · 2 comments

Comments

@elhedran
Copy link

elhedran commented Apr 18, 2024

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

8.12.0

Ajv options object

none

import Ajv, { JTDSchemaType } from 'ajv/dist/jtd'

const ajv = new Ajv()

JSON Schema

export interface IRequest {
    work_type: string,
    meta?: Record<string, unknown>
}

const RequestJDTSchema: JTDSchemaType<IRequest> = {
    properties: {
        work_type: { type: 'string' },
    },
    optionalProperties: {
        meta: { values: {}, nullable: false  },
    }
}

Sample data

{
  "work_type": "example.integration.2',"
  "meta": {
    "picks": [],
    "picks_error": []
  }
}

Your code

NOTE: could not reproduce this in Jest, only in the browser - as such the code is a Lit element that allows interaction to reproduce the issue. I could not work out how to write a standalone automated test to produce the same error.

import { LitElement, html, nothing } from 'lit'
import { customElement, state } from 'lit/decorators.js'
import Ajv, { JTDSchemaType } from 'ajv/dist/jtd'

const ajv = new Ajv()

export interface IRequest {
    work_type: string,
    meta?: Record<string, unknown>
}

const RequestJDTSchema: JTDSchemaType<IRequest> = {
    properties: {
        work_type: { type: 'string' },
    },
    optionalProperties: {
        meta: { values: {}, nullable: false  },
    }
}

export const serializeRequest = ajv.compileSerializer<IRequest>(RequestJDTSchema)

export const parseRequest = ajv.compileParser<IRequest>(RequestJDTSchema)

const jsonTextBad = serializeRequest({
  work_type: 'example.integration.2',
  meta: {
    picks: [],
    picks_error: []
  }
})

const jsonTextGood = serializeRequest({
  work_type: 'example.integration.2',
})

/**
 * An example element.
 *
 * @slot - This element has a slot
 * @csspart button - The button
 */
@customElement('my-element')
export class MyElement extends LitElement {

  @state()
  private errorText: string | undefined
  private request: IRequest | undefined

  render() {
    return html`
      <div>
      <button @click=${this._onBad}>attempt validation of fail case</button>
      <button @click=${this._onGood}>attempt validation of success case</button>
        ${this.errorText
        ? html`<h3>error text</h3>
        <pre>${this.errorText}</pre>`
        : nothing
      }
      ${this.request
        ? html`<h3>request</h3>
        <pre>${JSON.stringify(this.request, null, 2)}</pre>`
        : nothing
      }
      </div>
    `
  }

  private _onGood() {
    this.errorText = undefined
    this.request = parseRequest(jsonTextGood)
    if (!this.request) {
      this.errorText = `${parseRequest.position}: ${parseRequest.message}`
    }
  }

  private _onBad() {
    this.errorText = undefined
    this.request = parseRequest(jsonTextBad)
    if (!this.request) {
      this.errorText = `${parseRequest.position}: ${parseRequest.message}`
    }
  }
}

declare global {
  interface HTMLElementTagNameMap {
    'my-element': MyElement
  }
}

Validation result, data AFTER validation, error messages

74: unexpected end

What results did you expect?

Json was valid (especially given it is created via the compiled serializer)

Are you going to resolve the issue?

I honestly have no idea how this might be resolved. For now I'm working around it in my code by not using compileParser for this particular schema.

@elhedran
Copy link
Author

I should also admit I'm new to the JTDSchemaType - so I'm not going to take offense if it turns out the bug is in my code.
The version of the browser I was reproducing this in most recently is Edge: Version 123.0.2420.97 (Official build) (64-bit)

@jasoniangreen
Copy link
Collaborator

Hi there, is there any chance you can create a simpler failing example in runkit without using other dependencies (like Lit)? It would simplify understanding and debugging greatly.

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

No branches or pull requests

2 participants