-
Notifications
You must be signed in to change notification settings - Fork 157
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
Make use of $ref in JSON Schema #34
Comments
@Remirror-zz Hi I like the idea, however there isn't any way to trivially resolve the One option may be to extend TypeBox to support something like... const Person = Type.Object({
'name': Type.String(),
'age': Type.Number()
}, { ref: 'person' }); // new
const ChessMatch = Type.Object({
'playerWhite': Person, // check here if Person has a 'ref', and if so assign it to "definitions"
'playerBlack': Person
}); The only reservation I'd have about implementing this is it may add a fair amount of complexity to the current schema generation. But id be curious to see an implementation. Would you be interested in having a go implementing a reference design for this feature? I guess the make or break depends on just how intuitive the schema generation turns out to be for Let me know! |
Good point, but I think that using Additional difficulties will arise though if one wants to harness the actual power of For my case, I have now decided to go the other way round and put the priority on JSON schema rather than on TypeScript and thus to create my schema files manually and have them converted to interfaces by approaches such as |
@Remirror-zz Cool, thanks for the suggestion mate, its a good one. Ill leave this issue up for a while and give the feature some thought. TypeBox is due for some updates to align with some new features in TS 4.0, so may give this some thought then when I get around to those updates. Thanks again! |
I want this functionality too. But The problem is - TypeBox doesn't know which schema is to be exported. You'll have to create your own "Schema Container" (or "Schema Storage"), traverse entire definitions, and replace "referenced" schemas to JSONSchema reference object ( |
@Remirror-zz Hi. This issue has been outstanding for a while. I have recently pushed an update to TypeBox to support Note that As such, your original example can be expressed as. const Person = Type.Object({ name: Type.String(), age: Type.Number() })
const Common = Type.Box('https://chess.app.com/common', {
Person
})
const ChessMatch = Type.Object({
playerWhite: Type.Ref(Common, 'Person'),
playerBlack: Type.Ref(Common, 'Person'),
}); This produces the following two schemas. const Common = {
"$id": "https://chess.app.com/common",
"definitions": {
"Person": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "number"
}
},
"required": [
"name",
"age"
]
}
}
}
const ChessMatch = {
"type": "object",
"properties": {
"playerWhite": {
"$ref": "https://chess.app.com/common#/definitions/Person"
},
"playerBlack": {
"$ref": "https://chess.app.com/common#/definitions/Person"
}
},
"required": [
"playerWhite",
"playerBlack"
]
} For validation using AJV, you would now pass the const ajv = new Ajv()
ajv.addSchema(Common) // adds the `Person` definition to AJV
ajv.validate(ChessMatch, {
playerWhite: { name: 'dave', age: 42 },
playerBlack: { name: 'alice', age: 28 },
}) Note that this feature provides a form of Will close off this issue, but feel free to re-open if you have additional thoughts on the current implementation. |
outputs
This means, the definition of
Person
is repeated in the output. Instead, please consider using the$ref
keyword. The output would then look something like this:The text was updated successfully, but these errors were encountered: