Comments (3)
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.
Hey, can you show how you work with JSON Schemas?
from openapi-ts.
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)
- @hey-api/client-fetch: TS type RequestResult is not exported
- Option for config filename HOT 1
- Using Operation object with the methodNameGenarator HOT 1
- Transform generation fails if route has multiple success responses HOT 8
- `Spread types may only be created from object types` issue for additionalHeaders in generated `requests.ts` HOT 7
- Setting user-agent HOT 5
- Fetch Response Interceptor Does Not Intercept 500s HOT 5
- incorrect type generated when combining oneOf with properties HOT 1
- @hey-api/client-fetch missing CancelablePromise, how do I abort/cancel an in progress call?
- FormData/RequestBody not handled correctly with the standalone clients
- Add this project to OpenAPi Tools HOT 5
- Ability to customize service operation parameter name HOT 2
- Order of imports HOT 2
- OpenAPI parameter name of only an underscore results in missing parameter name in services.gen.ts and malformed type definition
- Add tutorial to docs
- Remote $ref support
- Generate query parameters when service generation is disabled HOT 9
- Support async transformers HOT 7
- Ignore asClass for named clients
- TypeScript error: No overload matches this call. HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from openapi-ts.