Giter Site home page Giter Site logo

Comments (3)

mrlubos avatar mrlubos commented on September 21, 2024 1

Hey, just want to give you an update so it doesn't look like I ghosted. Totally valid request and will be addressed at some point, it's just not a priority right now

from openapi-ts.

mrlubos avatar mrlubos commented on September 21, 2024

Hey, can you show how you work with JSON Schemas?

from openapi-ts.

lsdch avatar lsdch commented on September 21, 2024

Sure, I work with Vue / Vuetify and the specific use case is implementing a composable for forms to use the schema for input documentation and validation.

For example:

// Adapted from openapi-ts src
export type Schema = Readonly<{
  additionalProperties?: (boolean | Schema)
  allOf?: Readonly<Schema[]>
  anyOf?: Readonly<Schema[]>
  const?: string | number | boolean | null
  default?: unknown
  deprecated?: boolean
  description?: string
  enum?: Readonly<(string | number)[]>
  example?: unknown
  exclusiveMaximum?: boolean
  exclusiveMinimum?: boolean
  format?: string // removed the union type here as the generated formats can be other things, e.g. 'uri'
  items?: Schema
  maximum?: number
  maxItems?: number
  maxLength?: number
  maxProperties?: number
  minimum?: number
  minItems?: number
  minLength?: number
  minProperties?: number
  multipleOf?: number
  not?: Readonly<Schema[]>
  nullable?: boolean
  oneOf?: Readonly<Schema[]>
  pattern?: string
  properties?: Readonly<Record<string, Schema>>
  readOnly?: boolean
  required?: Readonly<string[]>
  title?: string
  type?: string | Readonly<string[]>
  uniqueItems?: boolean
  writeOnly?: boolean
}>


export type SchemaProperties = Readonly<Record<string, {}>>
export type SchemaWithProperties<P> = Schema & Readonly<{ type: "object", properties: P }>
export function useSchema<P extends SchemaProperties>(schema: SchemaWithProperties<P>) {
  function inputProps(key: keyof P) {
    const k = key as unknown as string
    const s = schema.properties?.[k]

    return {
      hint: s?.description,
      min: s?.minimum,
      max: s?.maximum,
      rules: schema.required?.includes(k)
        ? [
          (value: any) => value || value === 0 ? true : "This field is required"
        ]
        : undefined
    }
  }

  return { schema: inputProps }
}

This is used to inject the schema function into form components, which can be used to configure fields based on their specification in the schema, e.g. (vuetify form component) :

<template>
<v-form>
  <v-text-field label="Book title" v-bind="schema('title')"/>
  <v-text-field label="Author" v-bind="schema('author')"/>
  <v-text-field type="number" label="Quantity" v-bind="schema('quantity')"/>
</v-form>
</template>
<script setup lang="ts"> 
// [ ... various imports ]
const { schema } = useSchema($MyBookStoreSchema)
</script>

The JSON schema also helps providing type safety for the arguments of the injected schema function, as a union type of the property names in the schema.

Btw, I just realized my initial comment does not accurately describe the feature request: rather than exposing the OpenApiSchema interface as it is defined in openapi-ts source code, it's actually about exporting a common interface that the generated schemas implement.

from openapi-ts.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.