Search code examples
javascriptjsontypescript

Typescript Type of JSON schema object


Is there a special type associated with JSON-schema objects in typescript? My class has a method that checks whether its members satisfy the dynamic json schema schema, for now I do it like so,

<!-- language: typescript -->

verifySchema(schema: object): void {
    // do verification
}

where for example

<!-- language: typescript -->

const schema = {
  title: 'blabla',
  description: 'Basic schema',
  type: 'object',
  properties: {
    "firstName": {
    "type": "string",
    "description": "The person's first name."
    },
    "lastName": {
    "type": "string",
    "description": "The person's last name."
    },
...
}

But to remain generic I would like to allow for checking arbitrary json schemas, not just this specific one. Is it okay to set schema: object or are there best practices for JSON-schema objects?


Solution

  • You can use @types/json-schema.

    Then:

    import {JSONSchema7} from 'json-schema';
    
    verifySchema(schema: JSONSchema7): void {
        // do verification
    }