openapi-typescript-validator
Advanced tools
Comparing version 0.1.1 to 0.2.0
@@ -65,3 +65,2 @@ "use strict"; | ||
var templates_1 = require("./templates"); | ||
var object_hash_1 = require("object-hash"); | ||
var fs_1 = require("fs"); | ||
@@ -74,3 +73,3 @@ var js_yaml_1 = __importDefault(require("js-yaml")); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var directory, name, schemaFile, schemaType, prettierOptions, schema, properties, definitions, schemaJsonOutput, rawTypescriptModels, typescriptModels, validators, rawDecoderOutput, decoderOutput; | ||
var directory, name, schemaFile, schemaType, prettierOptions, schema, properties, definitions, schemaJsonOutput, compiledSchema, rawTypescriptModels, typescriptModels, decoders, rawDecoderOutput, decoderOutput; | ||
return __generator(this, function (_b) { | ||
@@ -104,8 +103,9 @@ switch (_b.label) { | ||
case 1: | ||
rawTypescriptModels = (_b.sent()) | ||
.replace(/\/\*\*[^]*?\*\//, '') | ||
compiledSchema = _b.sent(); | ||
rawTypescriptModels = templates_1.modelsTemplate | ||
.replace(/\$Models/g, compiledSchema) | ||
.replace(/\s*\[k: string\]: unknown;/g, '') // Allow additional properties in schema but not in typescript | ||
.replace(/export interface Schema \{[^]*?\n\}/, ''); | ||
typescriptModels = prettier_1.format(rawTypescriptModels, prettierOptions); | ||
validators = Object.entries(definitions) | ||
decoders = Object.entries(definitions) | ||
.filter(function (_a) { | ||
@@ -122,6 +122,5 @@ var name = _a[0], definition = _a[1]; | ||
.join('\n'); | ||
rawDecoderOutput = templates_1.rootTemplate | ||
rawDecoderOutput = templates_1.decodersTemplate | ||
.replace(/\$SchemaName/g, name) | ||
.replace(/\$SchemaHash/g, object_hash_1.sha1(schemaJsonOutput).substr(-6)) | ||
.replace(/\$Validators/g, validators); | ||
.replace(/\$Decoders/g, decoders); | ||
decoderOutput = prettier_1.format(rawDecoderOutput, prettierOptions); | ||
@@ -128,0 +127,0 @@ fs_1.mkdirSync(directory, { recursive: true }); |
@@ -1,3 +0,4 @@ | ||
export declare const rootTemplate = "\n/* eslint-disable */\n/* tslint-disable */\nimport Ajv, { ErrorObject } from 'ajv';\nimport schema from './$SchemaName-schema.json';\nimport * as types from './$SchemaName-models'\n\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// !!! AUTO GENERATED CODE, DON'T TOUCH !!!\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// const schemaHash = '$SchemaHash';\n\nconst ajv = new Ajv();\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, className: string): any {\n const schema = ajv.getSchema(schemaRef);\n if (!schema) {\n throw new Error(`Schema ${schemaRef} not found`);\n }\n\n const jsonObject = typeof json === 'string' ? JSON.parse(json) : json;\n\n if (schema(jsonObject)) {\n return jsonObject;\n }\n\n const jsonPreviewStr = (typeof json === 'string' ? json : JSON.stringify(jsonObject)).substring(0, 100);\n if (schema.errors) {\n throw Error(`${className} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${className} Unexpected data received. JSON: ${jsonPreviewStr}`);\n}\n\nfunction errorsText(errors: ErrorObject[]): string {\n return errors.map(error => `${error.dataPath}: ${error.message}`).join('\\n')\n}\n\n// Decoders\n$Validators\n"; | ||
export declare const decodersTemplate = "\n/* eslint-disable */\n/* tslint-disable */\nimport Ajv, { ErrorObject } from 'ajv';\nimport schema from './$SchemaName-schema.json';\nimport * as types from './$SchemaName-models'\n\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// !!! AUTO GENERATED CODE, DON'T TOUCH !!!\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nconst ajv = new Ajv();\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, className: string): any {\n const schema = ajv.getSchema(schemaRef);\n if (!schema) {\n throw new Error(`Schema ${schemaRef} not found`);\n }\n\n const jsonObject = typeof json === 'string' ? JSON.parse(json) : json;\n\n if (schema(jsonObject)) {\n return jsonObject;\n }\n\n const jsonPreviewStr = (typeof json === 'string' ? json : JSON.stringify(jsonObject)).substring(0, 100);\n if (schema.errors) {\n throw Error(`${className} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${className} Unexpected data received. JSON: ${jsonPreviewStr}`);\n}\n\nfunction errorsText(errors: ErrorObject[]): string {\n return errors.map(error => `${error.dataPath}: ${error.message}`).join('\\n')\n}\n\n// Decoders\n$Decoders\n"; | ||
export declare const decoderTemplate = "\nexport class $ClassDecoder {\n public static schemaRef: string = '#/definitions/$Class'\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $ClassDecoder.schemaRef, '$Class');\n }\n}\n"; | ||
export declare const modelsTemplate = "\n/* eslint-disable */\n\n$Models\n"; | ||
export declare const schemaJsonTemplate = "$SchemaOutput"; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.schemaJsonTemplate = exports.decoderTemplate = exports.rootTemplate = void 0; | ||
exports.rootTemplate = "\n/* eslint-disable */\n/* tslint-disable */\nimport Ajv, { ErrorObject } from 'ajv';\nimport schema from './$SchemaName-schema.json';\nimport * as types from './$SchemaName-models'\n\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// !!! AUTO GENERATED CODE, DON'T TOUCH !!!\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// const schemaHash = '$SchemaHash';\n\nconst ajv = new Ajv();\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, className: string): any {\n const schema = ajv.getSchema(schemaRef);\n if (!schema) {\n throw new Error(`Schema ${schemaRef} not found`);\n }\n\n const jsonObject = typeof json === 'string' ? JSON.parse(json) : json;\n\n if (schema(jsonObject)) {\n return jsonObject;\n }\n\n const jsonPreviewStr = (typeof json === 'string' ? json : JSON.stringify(jsonObject)).substring(0, 100);\n if (schema.errors) {\n throw Error(`${className} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${className} Unexpected data received. JSON: ${jsonPreviewStr}`);\n}\n\nfunction errorsText(errors: ErrorObject[]): string {\n return errors.map(error => `${error.dataPath}: ${error.message}`).join('\\n')\n}\n\n// Decoders\n$Validators\n"; | ||
exports.schemaJsonTemplate = exports.modelsTemplate = exports.decoderTemplate = exports.decodersTemplate = void 0; | ||
exports.decodersTemplate = "\n/* eslint-disable */\n/* tslint-disable */\nimport Ajv, { ErrorObject } from 'ajv';\nimport schema from './$SchemaName-schema.json';\nimport * as types from './$SchemaName-models'\n\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n// !!! AUTO GENERATED CODE, DON'T TOUCH !!!\n// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\nconst ajv = new Ajv();\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, className: string): any {\n const schema = ajv.getSchema(schemaRef);\n if (!schema) {\n throw new Error(`Schema ${schemaRef} not found`);\n }\n\n const jsonObject = typeof json === 'string' ? JSON.parse(json) : json;\n\n if (schema(jsonObject)) {\n return jsonObject;\n }\n\n const jsonPreviewStr = (typeof json === 'string' ? json : JSON.stringify(jsonObject)).substring(0, 100);\n if (schema.errors) {\n throw Error(`${className} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${className} Unexpected data received. JSON: ${jsonPreviewStr}`);\n}\n\nfunction errorsText(errors: ErrorObject[]): string {\n return errors.map(error => `${error.dataPath}: ${error.message}`).join('\\n')\n}\n\n// Decoders\n$Decoders\n"; | ||
exports.decoderTemplate = "\nexport class $ClassDecoder {\n public static schemaRef: string = '#/definitions/$Class'\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $ClassDecoder.schemaRef, '$Class');\n }\n}\n"; | ||
exports.modelsTemplate = "\n/* eslint-disable */\n\n$Models\n"; | ||
exports.schemaJsonTemplate = "$SchemaOutput"; | ||
//# sourceMappingURL=templates.js.map |
{ | ||
"name": "openapi-typescript-validator", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "Generate typescript with ajv validation based on openapi schemas", | ||
@@ -26,4 +26,3 @@ "main": "dist/index.js", | ||
"js-yaml": "^4.0.0", | ||
"json-schema-to-typescript": "^10.1.3", | ||
"object-hash": "^2.1.1" | ||
"json-schema-to-typescript": "^10.1.3" | ||
}, | ||
@@ -30,0 +29,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
3
166
18971
- Removedobject-hash@^2.1.1
- Removedobject-hash@2.2.0(transitive)