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

Added function parameters definition in schema when using typeOf keyword #504

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

Tiberiu02
Copy link

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#"
}

@Tiberiu02
Copy link
Author

@YousefED Do you think you could you take a look over this? It would be very useful for our project

@domoritz
Copy link
Collaborator

How does this compare with what ts-json-schema-generator does?

@Tiberiu02
Copy link
Author

Didn't know about ts-json-schema-generator, let me take a look

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

Successfully merging this pull request may close these issues.

2 participants