New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

openapi-typescript-validator

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

openapi-typescript-validator - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

215

dist/builder.d.ts
import { JSONSchema } from 'json-schema-to-typescript';
declare type SchemaObject = JSONSchema;
declare type SchemaObjectOrRef = SchemaObject | string;
export declare const boolean: {
type: string;
};
export declare const id: {
type: string;
minLength: number;
};
export declare const positiveInteger: {
type: string;
minimum: number;
};
export declare const string: {
type: string;
};
export declare const number: {
type: string;
};
export declare const date: {
type: string;
format: string;
};
export declare const dateTime: {
type: string;
format: string;
};
export declare const uri: {
type: string;
format: string;
};
export declare const email: {
type: string;
format: string;
};
export declare const any: {};
export declare const anonymousData: {
additionalProperties: {
type: string;
};
};
export declare const object: (properties: Record<string, any>) => {
type: string;
properties: Record<string, any>;
required: string[];
};
export declare const ref: (refName: string) => {
$ref: string;
};
export declare const array: (itemType: SchemaObjectOrRef) => {
type: string;
items: JSONSchema | {
$ref: string;
};
};
export declare const map: (itemType: SchemaObjectOrRef) => {
type: string;
patternProperties: {
'.*': JSONSchema | {
$ref: string;
};
};
additionalProperties: boolean;
};
interface CustomSchemaObject {
kind: 'custom';
object: SchemaObject;
optional?: boolean;
}
declare type PropertyValue = SchemaObject | CustomSchemaObject;
export declare const boolean: SchemaObject;
export declare const id: SchemaObject;
export declare const positiveInteger: SchemaObject;
export declare const string: SchemaObject;
export declare const number: SchemaObject;
export declare const date: SchemaObject;
export declare const dateTime: SchemaObject;
export declare const uri: SchemaObject;
export declare const email: SchemaObject;
export declare const any: SchemaObject;
export declare const anonymousData: SchemaObject;
export declare const object: (properties: Record<string, PropertyValue>) => SchemaObject;
export declare const ref: (refName: string) => SchemaObject;
export declare const array: (itemType: SchemaObjectOrRef) => SchemaObject;
export declare const map: (itemType: SchemaObjectOrRef) => SchemaObject;
export declare const nullable: (type: SchemaObjectOrRef) => SchemaObject;
export declare const nillable: (type: SchemaObjectOrRef) => {
$optional: boolean;
tsEnumNames?: string[] | undefined;
tsType?: string | undefined;
id?: string | undefined;
$ref?: string | undefined;
$schema?: string | undefined;
title?: string | undefined;
description?: string | undefined;
default?: import("json-schema").JSONSchema4Type | undefined;
multipleOf?: number | undefined;
maximum?: number | undefined;
exclusiveMaximum?: boolean | undefined;
minimum?: number | undefined;
exclusiveMinimum?: boolean | undefined;
maxLength?: number | undefined;
minLength?: number | undefined;
pattern?: string | undefined;
additionalItems?: boolean | import("json-schema").JSONSchema4 | undefined;
items?: import("json-schema").JSONSchema4 | import("json-schema").JSONSchema4[] | undefined;
maxItems?: number | undefined;
minItems?: number | undefined;
uniqueItems?: boolean | undefined;
maxProperties?: number | undefined;
minProperties?: number | undefined;
required?: false | string[] | undefined;
additionalProperties?: boolean | import("json-schema").JSONSchema4 | undefined;
definitions?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
properties?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
patternProperties?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
dependencies?: {
[k: string]: import("json-schema").JSONSchema4 | string[];
} | undefined;
enum?: import("json-schema").JSONSchema4Type[] | undefined;
type?: import("json-schema").JSONSchema4TypeName | import("json-schema").JSONSchema4TypeName[] | undefined;
allOf?: import("json-schema").JSONSchema4[] | undefined;
anyOf?: import("json-schema").JSONSchema4[] | undefined;
oneOf?: import("json-schema").JSONSchema4[] | undefined;
not?: import("json-schema").JSONSchema4 | undefined;
extends?: string | string[] | undefined;
format?: string | undefined;
} | {
$optional: boolean;
$ref: string;
};
export declare const optional: (type: SchemaObjectOrRef) => {
$optional: boolean;
tsEnumNames?: string[] | undefined;
tsType?: string | undefined;
id?: string | undefined;
$ref?: string | undefined;
$schema?: string | undefined;
title?: string | undefined;
description?: string | undefined;
default?: import("json-schema").JSONSchema4Type | undefined;
multipleOf?: number | undefined;
maximum?: number | undefined;
exclusiveMaximum?: boolean | undefined;
minimum?: number | undefined;
exclusiveMinimum?: boolean | undefined;
maxLength?: number | undefined;
minLength?: number | undefined;
pattern?: string | undefined;
additionalItems?: boolean | import("json-schema").JSONSchema4 | undefined;
items?: import("json-schema").JSONSchema4 | import("json-schema").JSONSchema4[] | undefined;
maxItems?: number | undefined;
minItems?: number | undefined;
uniqueItems?: boolean | undefined;
maxProperties?: number | undefined;
minProperties?: number | undefined;
required?: false | string[] | undefined;
additionalProperties?: boolean | import("json-schema").JSONSchema4 | undefined;
definitions?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
properties?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
patternProperties?: {
[k: string]: import("json-schema").JSONSchema4;
} | undefined;
dependencies?: {
[k: string]: import("json-schema").JSONSchema4 | string[];
} | undefined;
enum?: import("json-schema").JSONSchema4Type[] | undefined;
type?: import("json-schema").JSONSchema4TypeName | import("json-schema").JSONSchema4TypeName[] | undefined;
allOf?: import("json-schema").JSONSchema4[] | undefined;
anyOf?: import("json-schema").JSONSchema4[] | undefined;
oneOf?: import("json-schema").JSONSchema4[] | undefined;
not?: import("json-schema").JSONSchema4 | undefined;
extends?: string | string[] | undefined;
format?: string | undefined;
} | {
$optional: boolean;
$ref: string;
};
export declare const oneOf: (types: SchemaObjectOrRef[]) => {
oneOf: (JSONSchema | {
$ref: string;
})[];
};
export declare const anyOf: (types: SchemaObjectOrRef[]) => {
anyOf: (JSONSchema | {
$ref: string;
})[];
};
export declare const enumerate: (values: string[]) => {
type: string;
enum: string[];
};
export declare const constant: (value: string) => {
type: string;
enum: string[];
};
export declare const compose: (...sources: SchemaObject[]) => {
type: string;
properties: Record<string, any>;
required: string[];
};
export declare const nillable: (type: SchemaObjectOrRef) => SchemaObject;
export declare const optional: (type: SchemaObjectOrRef) => CustomSchemaObject;
export declare const oneOf: (types: SchemaObjectOrRef[]) => SchemaObject;
export declare const anyOf: (types: SchemaObjectOrRef[]) => SchemaObject;
export declare const enumerate: (values: string[]) => SchemaObject;
export declare const constant: (value: string) => SchemaObject;
export declare const compose: (...sources: SchemaObject[]) => SchemaObject;
export {};

32

dist/builder.js

@@ -31,7 +31,24 @@ "use strict";

exports.anonymousData = { additionalProperties: { type: 'string' } };
var object = function (properties) { return ({
type: 'object',
properties: properties,
required: Object.keys(properties).filter(function (key) { return !properties[key].$optional; }),
}); };
var object = function (properties) {
var required = [];
var schemaProperties = {};
Object.entries(properties).forEach(function (_a) {
var key = _a[0], property = _a[1];
if (property.kind === 'custom') {
if (!property.optional) {
required.push('key');
}
schemaProperties[key] = property.object;
}
else {
required.push(key);
schemaProperties[key] = property;
}
});
return {
type: 'object',
properties: schemaProperties,
required: required
};
};
exports.object = object;

@@ -55,3 +72,4 @@ var ref = function (refName) { return ({

var obj = autoRef(type);
if ('type' in obj && obj.type && ['string', 'number', 'boolean', 'integer'].includes(obj.typy)) {
var types = ['string', 'number', 'boolean', 'integer'];
if (typeof obj.type === 'string' && types.includes(obj.type)) {
return __assign(__assign({}, obj), { type: [obj.type, 'null'] });

@@ -64,3 +82,3 @@ }

exports.nillable = nillable;
var optional = function (type) { return (__assign(__assign({}, autoRef(type)), { $optional: true })); };
var optional = function (type) { return ({ kind: 'custom', object: autoRef(type), optional: true }); };
exports.optional = optional;

@@ -67,0 +85,0 @@ var oneOf = function (types) { return ({ oneOf: types.map(autoRef) }); };

@@ -68,3 +68,3 @@ "use strict";

return __awaiter(this, void 0, void 0, function () {
var name, schemaFile, schemaType, prettierOptions, directories, schema, compiledSchema, rawTypescriptModels, typescriptModels, decoders, rawDecoderOutput, decoderOutput;
var name, schemaFile, schemaType, prettierOptions, directories, schema, compiledTypescriptModels, rawTypescriptModels, typescriptModels, decoders, rawDecoderOutput, decoderOutput;
return __generator(this, function (_b) {

@@ -77,8 +77,10 @@ switch (_b.label) {

console.info("Start generating files for " + schemaType + " schema: " + schemaFile);
schema = parse_schema_1.parseSchema(schemaFile, schemaType);
return [4 /*yield*/, parse_schema_1.parseSchema(schemaFile, schemaType)];
case 1:
schema = _b.sent();
return [4 /*yield*/, json_schema_to_typescript_1.compile(JSON.parse(schema.json), 'Schema')];
case 1:
compiledSchema = _b.sent();
case 2:
compiledTypescriptModels = _b.sent();
rawTypescriptModels = templates_1.modelsTemplate
.replace(/\$Models/g, compiledSchema)
.replace(/\$Models/g, compiledTypescriptModels)
.replace(/\s*\[k: string\]: unknown;/g, '') // Allow additional properties in schema but not in typescript

@@ -85,0 +87,0 @@ .replace(/export interface Schema \{[^]*?\n\}/, '');

@@ -1,3 +0,3 @@

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): 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(`${schemaRef} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${schemaRef} 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 $DecoderName {\n public static definitionName: string = '$Class';\n public static schemaRef: string = '#/definitions/$Class';\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $DecoderName.schemaRef);\n }\n}\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({ strict: false });\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, definitionName: 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, 200);\n if (schema.errors) {\n throw Error(`${definitionName} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${definitionName} 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 $DecoderName {\n public static definitionName: string = '$Class';\n public static schemaRef: string = '#/definitions/$Class';\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $DecoderName.schemaRef, $DecoderName.definitionName);\n }\n}\n";
export declare const modelsTemplate = "\n/* eslint-disable */\n\n$Models\n";
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
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): 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(`${schemaRef} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${schemaRef} 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 $DecoderName {\n public static definitionName: string = '$Class';\n public static schemaRef: string = '#/definitions/$Class';\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $DecoderName.schemaRef);\n }\n}\n";
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({ strict: false });\najv.addSchema(schema);\n\nfunction validateJson(json: any, schemaRef: string, definitionName: 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, 200);\n if (schema.errors) {\n throw Error(`${definitionName} ${errorsText(schema.errors)}. JSON-preview: ${jsonPreviewStr}`);\n }\n\n throw Error(`${definitionName} 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 $DecoderName {\n public static definitionName: string = '$Class';\n public static schemaRef: string = '#/definitions/$Class';\n\n public static decode(json: any): types.$Class {\n return validateJson(json, $DecoderName.schemaRef, $DecoderName.definitionName);\n }\n}\n";
exports.modelsTemplate = "\n/* eslint-disable */\n\n$Models\n";
//# sourceMappingURL=templates.js.map
{
"name": "openapi-typescript-validator",
"version": "1.0.1",
"version": "1.1.0",
"description": "Generate typescript with ajv validation based on openapi schemas",

@@ -5,0 +5,0 @@ "main": "dist/index.js",

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc