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

Field descriptions #9

Closed
riquenunes opened this issue Mar 6, 2020 · 5 comments
Closed

Field descriptions #9

riquenunes opened this issue Mar 6, 2020 · 5 comments

Comments

@riquenunes
Copy link

Is it possible to use typebox to generate JSON schemas with field descriptions?

For example:

"properties": {
    "productId": {
      "description": "The unique identifier for a product",
      "type": "integer"
    }
  }
@sinclairzx81
Copy link
Owner

Hi. I have just pushed an update to allow for this (typebox npm version 0.9.7). I have added documentation for this use case in the readme. See User Defined Schema Properties.

Note: To allow for custom properties across all types. The signatures of Type.Union(...), Type.Intersect(...) and Type.Tuple(...), have changed.

// before
const T = Type.Tuple(Type.String(), Type.Number()) // [string, number]

// now
const T = Type.Tuple([Type.String(), Type.Number()])

// allowing for
const T = Type.Tuple([Type.String(), Type.Number()], { desc: 'a tuple' })

Hope that helps !

@riquenunes
Copy link
Author

thanks! that's exactly what I needed!

@rattrayalex
Copy link

The linked section no longer appears in the readme, and I don't find any mention of title, description, or other annotations allowed in the json-schema spec.

Was this feature removed?

@sinclairzx81
Copy link
Owner

@rattrayalex You can pass additional options (including title and description) on the last argument for any given type. For example.

const T = Type.String({ title: 'String', description: 'A basic string type' })

const T = Type.Object({
   email: Type.String({ format: 'email' }),
   address: Type.String({ description: 'US street address' })
}, {title: 'Customer', description: 'A customer record' })

TypeBox still provides definitions for all the known JSON schema options (so you should see these using intellisense), but you can specify your own custom properties if you wish. All properties will be applied to the underlying schema, except for properties required by the underlying type (which will be ignored, or more specifically overwritten when constructing the schema).

Hope that helps.
S

@rattrayalex
Copy link

rattrayalex commented Aug 9, 2021

Got it! Very helpful. Can you add custom properties this way as well, like 'x-mycompany-myannotation': "foo"?

EDIT: I've put up a PR to the readme to clarify, assuming the answer to the above is "yes": #98

rattrayalex added a commit to rattrayalex/typebox that referenced this issue Aug 11, 2021
sinclairzx81#9 (comment)

I had searched for "title", "description", "document", and "nullable" in search of how to add annotations.  
Many JSON Schema users use it for OpenAPI, which has the `nullable` property that isn't in JSON Schema, and may be curious how to use it. They may also wish to know whether extensions like `x-mycompany-myattribute` work.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants