-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using ts-json-schema-generator to generate schema from TypeScript (#78)
- Loading branch information
Showing
4 changed files
with
82 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
node_modules | ||
**.js | ||
**.d.ts | ||
schema.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"definitions": { | ||
"Company": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"jobs": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"dataScientist": { | ||
"$ref": "#/definitions/Job" | ||
}, | ||
"productManager": { | ||
"$ref": "#/definitions/Job" | ||
}, | ||
"softwareEngineer": { | ||
"$ref": "#/definitions/Job" | ||
} | ||
}, | ||
"type": "object" | ||
}, | ||
"url": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"jobs" | ||
], | ||
"type": "object" | ||
}, | ||
"Job": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"intern": { | ||
"$ref": "#/definitions/Opening" | ||
}, | ||
"manager": { | ||
"$ref": "#/definitions/Opening" | ||
}, | ||
"newGrad": { | ||
"$ref": "#/definitions/Opening" | ||
}, | ||
"senior": { | ||
"$ref": "#/definitions/Opening" | ||
} | ||
}, | ||
"type": "object" | ||
}, | ||
"JobsData": { | ||
"additionalProperties": { | ||
"$ref": "#/definitions/Company" | ||
}, | ||
"type": "object" | ||
}, | ||
"Opening": { | ||
"additionalProperties": false, | ||
"properties": { | ||
"available": { | ||
"type": "boolean" | ||
}, | ||
"url": { | ||
"type": "string" | ||
} | ||
}, | ||
"required": [ | ||
"available" | ||
], | ||
"type": "object" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,60 +1,12 @@ | ||
import Ajv, { JSONSchemaType, DefinedError } from "ajv"; | ||
import type { Opening, Job, Company, JobsData } from "./index"; | ||
import data from "./data.json" assert { type: "json" }; | ||
|
||
const openingSchema: JSONSchemaType<Opening> = { | ||
type: "object", | ||
required: ["available"], | ||
properties: { | ||
available: { type: "boolean" }, | ||
url: { type: "string", nullable: true }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
|
||
const jobSchema: JSONSchemaType<Job> = { | ||
type: "object", | ||
required: [], | ||
properties: { | ||
intern: { ...openingSchema, nullable: true }, | ||
newGrad: { ...openingSchema, nullable: true }, | ||
senior: { ...openingSchema, nullable: true }, | ||
manager: { ...openingSchema, nullable: true }, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
|
||
const companySchema: JSONSchemaType<Company> = { | ||
type: "object", | ||
required: ["jobs"], | ||
properties: { | ||
url: { type: "string", nullable: true }, | ||
jobs: { | ||
type: "object", | ||
required: [], | ||
properties: { | ||
softwareEngineer: { ...jobSchema, nullable: true }, | ||
dataScientist: { ...jobSchema, nullable: true }, | ||
productManager: { ...jobSchema, nullable: true }, | ||
}, | ||
additionalProperties: false, | ||
}, | ||
}, | ||
additionalProperties: false, | ||
}; | ||
|
||
const schema: JSONSchemaType<JobsData> = { | ||
type: "object", | ||
required: [], | ||
additionalProperties: companySchema, | ||
}; | ||
|
||
// @ts-ignore | ||
const ajv: Ajv = new Ajv.default(); | ||
const validate = ajv.compile(schema); | ||
if (!validate(data)) { | ||
for (const error of validate.errors as DefinedError[]) { | ||
import Ajv, { DefinedError, Schema } from 'ajv'; | ||
import data from './data.json' assert { type: 'json' }; | ||
import schemas from './schemas.json' assert { type: 'json' }; | ||
|
||
const ajv = new Ajv(); | ||
ajv.addSchema(schemas as Schema); | ||
if (!ajv.validate('#/definitions/JobsData', data)) { | ||
for (const error of ajv.errors as DefinedError[]) { | ||
console.error(error); | ||
} | ||
throw new Error("Validation failure"); | ||
throw new Error('Validation failure'); | ||
} |