typescript-json-schema
typescript-json-schema copied to clipboard
Added function parameters definition in schema when using `typeOf` keyword
When using the typeOf keyword, type definitions of all functions will now have a "parameters" field containing a list of the type definitions of all function parameters.
This is very useful for fully automated runtime type checking in RPC architectures.
Example:
Code: (Note the use of various function syntaxes)
function sum(a: number, b: number) {
return a + b;
}
function prod(a: number, b: number) {
return a + b;
}
const length = (s: string) => s.length;
const extractAttribute = (obj: object, attribute: string) => obj[attribute];
class Arrays {
static get(array: number[], index: number) {
return array[index];
}
}
export const API = {
numbers: {
sum,
prod
},
objects: {
length,
extractAttribute,
extractArrayEl: Arrays.get
}
};
Produced schema: (note the parameters lists)
{
"type": "object",
"properties": {
"api": {
"type": "object",
"properties": {
"numbers": {
"type": "object",
"properties": {
"sum": {
"typeof": "function",
"parameters": [
{
"type": "number"
},
{
"type": "number"
}
]
},
"prod": {
"typeof": "function",
"parameters": [
{
"type": "number"
},
{
"type": "number"
}
]
}
}
},
"objects": {
"type": "object",
"properties": {
"length": {
"typeof": "function",
"parameters": [
{
"type": "string"
}
]
},
"extractAttribute": {
"typeof": "function",
"parameters": [
{
"type": "object",
"properties": {},
"additionalProperties": true
},
{
"type": "string"
}
]
},
"extractArrayEl": {
"typeof": "function",
"parameters": [
{
"type": "array",
"items": {
"type": "number"
}
},
{
"type": "number"
}
]
}
}
}
}
}
},
"$schema": "http://json-schema.org/draft-07/schema#"
}
@YousefED Do you think you could you take a look over this? It would be very useful for our project
How does this compare with what ts-json-schema-generator does?
Didn't know about ts-json-schema-generator, let me take a look