Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

angular
schema

How to use schema.json and schema.d.ts files to define a schema

Syed Muhammad Ali Mustafa

schema.json

JSON schema is a vocabulary that tests and validates JSON documents. It is mainly used to describe the structure of a JSON document and its validation constraints.

Benefits

The benefits of using the schema.json file are demonstrated in the following illustration:

Example

Consider the example of JSON schema of a Student entity:

{
"type": "object",
  "properties": { 
    "Roll_No": {
      "type": "string"
    },
    "First_Name": {
      "type": "string"
    },
    "Last_Name": {
      "type": "string"
    },
    "Email_Address": {
      "type": "string",
      "format": "email"
    },
    "gender": {
      "type": "string",
      "enum": ["Male", "Female", "Other"]
    }
  },
  "required": [ "Roll_No", "First_Name" ],
  "additionalProperties": false
}

A valid instance against this schema is as follows:

{
  "Roll_No": "20K-4560",
  "First_Name": "Mary"
}

schema.d.ts

schema.d.ts contains the schematic variable definitions of a named schematic. These files only contain the type information of the schema.

Example

Consider the example of a *.d.ts file of an LMS schema. The schema contains only one entity i.e., Student, as shown below:

declare namespace LMSRaw {
    namespace RequestBodies {
        export type Students = Schemas.Student;
    }
    namespace Schemas {
        export interface Student {
         
            Roll_No?: string;
            
            First_Name?: string;
            
            Last_Name?: string;
            
            Email_Address?: string;

            gender?: string;
        }
        export interface ApiResponse {
            code?: number; // int32
            type?: string;
            message?: string;
        }
 
}
declare namespace LMSRawPaths {
    namespace AddStudent {
        export type RequestBody = LMSRaw.Schemas.Student;
        namespace Responses {
            export type $200 = LMSRaw.Schemas.Student;
            export interface $405 {
            }
        }
    }
}

RELATED TAGS

angular
schema

CONTRIBUTOR

Syed Muhammad Ali Mustafa
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring