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

How can I iterate over a JSON Schema using typescript? #2351

Open
johann1301s opened this issue Dec 5, 2023 · 1 comment
Open

How can I iterate over a JSON Schema using typescript? #2351

johann1301s opened this issue Dec 5, 2023 · 1 comment

Comments

@johann1301s
Copy link

I would like to implement a typescript function called iterateAllOfJsonSchema.

const iterateAllOfJsonSchema = (schema: GenericSchema) => {
  Object.entries(schema).reduce((acc, cur) => {
    if (cur.type === 'string') {  // <-- Can I get typescript intellisense here?
      // do stuff if string...
    } else if (cur.type === 'object') {
      // do stuff if object...
    }
   // ...etc
  })
}

Can I import a type similar to GenericSchema from Ajv? If not, where can I import it from? If it can't be imported, where can I begin to read the definitions so I can implement it myself? How do I make sure that the version of JSON Schema in the type GenericSchema is the same as the type exported from Ajv?


I tried importing JSONSchema7 from json-schema. But how can I know that 7 is the correct version matching Ajv? And is this library official?

import {JSONSchema7} from 'json-schema'

The ultimate goal is to create a React library, where I use the JSON Schema's of each component to show the props in a web view. Thus, I need to 'loop' over the JSON Schema.

@johann1301s
Copy link
Author

I tried using JSONSchemaType from Ajv.

import {JSONSchemaType} from 'ajv'

const iterateAllOfJsonSchema = (schema: JSONSchemaType<any>) => {

    switch (schema.type) {
        case '': { // <--- type is any here?

        }
    }
}

Does not give type intellisense...

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
@johann1301s and others