Skip to content

Commit 36a97b4

Browse files
authored
Merge pull request #2 from Rhosys/fix-string-literal-issues
Fix string literal issues
2 parents 7b29ab0 + 87cd1e7 commit 36a97b4

10 files changed

Lines changed: 136 additions & 200 deletions

File tree

.eslintrc

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
"node": true
55
},
66
"extends": [
7-
"cimpress-atsquad"
7+
"cimpress-atsquad",
8+
"eslint:recommended",
9+
"plugin:@typescript-eslint/eslint-recommended",
10+
"plugin:@typescript-eslint/recommended"
811
],
912
"plugins": [
1013
"@typescript-eslint",
@@ -26,9 +29,19 @@
2629
"no-continue": "off",
2730
"require-atomic-updates": "off",
2831
"no-constant-condition": ["error", { "checkLoops": false }],
32+
"node/no-missing-import": ["error", {
33+
"allowModules": [],
34+
"resolvePaths": [],
35+
"tryExtensions": [".js", ".ts", ".json", ".node"]
36+
}],
2937
"node/no-missing-require": ["error", {
30-
"allowModules": ["jose"]
31-
}]
38+
"allowModules": [],
39+
"resolvePaths": [],
40+
"tryExtensions": [".js", ".ts", ".json", ".node"]
41+
}],
42+
"@typescript-eslint/no-explicit-any": "off",
43+
"@typescript-eslint/no-var-requires": "off",
44+
"@typescript-eslint/no-empty-function": "off"
3245
}
3346
}
3447

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"cSpell.words": [
33
"apidevtools",
44
"expressjs",
5+
"middlewares",
56
"unopinionated"
67
]
78
}

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@
4747
"@types/ajv": "^1.0.0",
4848
"@types/mocha": "^8.2.2",
4949
"@types/node": "^15.3.1",
50-
"@typescript-eslint/eslint-plugin": "^5.0.0",
51-
"@typescript-eslint/parser": "^5.0.0",
50+
"@typescript-eslint/eslint-plugin": "^5.3.0",
51+
"@typescript-eslint/parser": "^5.3.0",
5252
"chai": "^4.3.4",
5353
"ci-build-tools": "^1.0.13",
5454
"commander": "^7.2.0",
5555
"coveralls": "^3.1.0",
5656
"error-object-polyfill": "^1.1.14",
57-
"eslint": "^7.20.0",
57+
"eslint": "^8.1.0",
5858
"eslint-config-cimpress-atsquad": "^1.0.67",
5959
"eslint-plugin-import": "^2.22.1",
6060
"eslint-plugin-mocha": "^8.0.0",

src/framework/ajv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export function createRequestAjv(
1616
logger: {
1717
log(...args) { console.log(...args); },
1818
warn(...args) {
19-
if (!arguments[0]?.match('jsPropertySyntax')) {
19+
if (!(args[0] as string)?.match('jsPropertySyntax')) {
2020
console.warn(...args);
2121
}
2222
},

src/framework/types.ts

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1-
/* eslint-disable no-unused-vars */
21
/* eslint-disable no-use-before-define */
3-
import { ErrorObject, Options as AjvOptions } from 'ajv';
2+
import { Options as AjvOptions } from 'ajv';
43

4+
/********
5+
Typescript string literals are broken and require this garbage to work: https://stackoverflow.com/questions/37978528/typescript-type-string-is-not-assignable-to-type so we'll break the allowed validation using typescript interfaces, we don't really care to use this for validation of the schema, so any place we use string literals we'll allow any "STRING" until it is fixed.
6+
=> When fixed search here for places with `'literal' | string` and remove the string part.
7+
*/
58
export type BodySchema =
69
| OpenAPIV3.ReferenceObject
710
| OpenAPIV3.SchemaObject
@@ -80,6 +83,7 @@ export interface OpenApiValidatorOpts {
8083
formats?: Format[];
8184
}
8285

86+
// eslint-disable-next-line @typescript-eslint/no-namespace
8387
export namespace OpenAPIV3 {
8488
export interface Document {
8589
openapi: string;
@@ -169,7 +173,7 @@ export namespace OpenAPIV3 {
169173
in: string;
170174
}
171175

172-
export interface HeaderObject extends ParameterBaseObject {}
176+
export type HeaderObject = ParameterBaseObject
173177

174178
interface ParameterBaseObject {
175179
description?: string;
@@ -184,23 +188,34 @@ export namespace OpenAPIV3 {
184188
examples?: { [media: string]: ReferenceObject | ExampleObject };
185189
content?: { [media: string]: MediaTypeObject };
186190
}
187-
export type NonArraySchemaObjectType =
188-
| 'null'
189-
| 'boolean'
190-
| 'object'
191-
| 'number'
192-
| 'string'
193-
| 'integer';
194-
export type ArraySchemaObjectType = 'array';
195-
export type SchemaObject = ArraySchemaObject | NonArraySchemaObject;
196191

192+
export type SchemaObject = ArraySchemaObject | NonArraySchemaObject | AllOfSchemaObject | OneOfSchemaObject | anyOfSchemaObject | notSchemaObject;
193+
194+
export interface AllOfSchemaObject extends BaseSchemaObject {
195+
allOf: Array<ReferenceObject | SchemaObject>;
196+
}
197+
198+
export interface OneOfSchemaObject extends BaseSchemaObject {
199+
oneOf: Array<ReferenceObject | SchemaObject>;
200+
}
201+
202+
export interface anyOfSchemaObject extends BaseSchemaObject {
203+
anyOf: Array<ReferenceObject | SchemaObject>;
204+
}
205+
206+
export interface notSchemaObject extends BaseSchemaObject {
207+
not: Array<ReferenceObject | SchemaObject>;
208+
}
209+
210+
export type ArraySchemaObjectType = 'array';
197211
export interface ArraySchemaObject extends BaseSchemaObject {
198-
type: ArraySchemaObjectType;
212+
type: ArraySchemaObjectType | string;
199213
items: ReferenceObject | SchemaObject;
200214
}
201215

216+
export type NonArraySchemaObjectType = 'null' | 'boolean' | 'object' | 'number' | 'string' | 'integer';
202217
export interface NonArraySchemaObject extends BaseSchemaObject {
203-
type: NonArraySchemaObjectType;
218+
type: NonArraySchemaObjectType | string;
204219
}
205220

206221
interface BaseSchemaObject {
@@ -228,10 +243,6 @@ export namespace OpenAPIV3 {
228243
properties?: {
229244
[name: string]: ReferenceObject | SchemaObject;
230245
};
231-
allOf?: Array<ReferenceObject | SchemaObject>;
232-
oneOf?: Array<ReferenceObject | SchemaObject>;
233-
anyOf?: Array<ReferenceObject | SchemaObject>;
234-
not?: ReferenceObject | SchemaObject;
235246

236247
// OpenAPI-specific properties
237248
nullable?: boolean;
@@ -339,21 +350,22 @@ export namespace OpenAPIV3 {
339350
| OpenIdSecurityScheme;
340351

341352
export interface HttpSecurityScheme {
342-
type: 'http';
353+
type: 'http' | string;
343354
description?: string;
344355
scheme: string;
345356
bearerFormat?: string;
346357
}
347358

348359
export interface ApiKeySecurityScheme {
349-
type: 'apiKey';
360+
type: 'apiKey' | string;
350361
description?: string;
351362
name: string;
352363
in: string;
353364
}
354365

355366
export interface OAuth2SecurityScheme {
356-
type: 'oauth2';
367+
type: 'oauth2' | string;
368+
description?: string;
357369
flows: {
358370
implicit?: {
359371
authorizationUrl: string;
@@ -380,7 +392,7 @@ export namespace OpenAPIV3 {
380392
}
381393

382394
export interface OpenIdSecurityScheme {
383-
type: 'openIdConnect';
395+
type: 'openIdConnect' | string;
384396
description?: string;
385397
openIdConnectUrl: string;
386398
}
@@ -481,9 +493,9 @@ interface ErrorHeaders {
481493
}
482494

483495
export class BadRequest extends Error implements ValidationError {
484-
status: number = 400;
496+
status = 400;
485497
path?: string;
486-
name: string = 'Bad Request';
498+
name = 'Bad Request';
487499
message!: string;
488500
headers?: ErrorHeaders;
489501
errors!: ValidationErrorItem[];

src/index.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { RequestValidator } from './middlewares/openapi.request.validator';
22
import {
33
OpenApiValidatorOpts,
4-
OpenApiRequest
4+
OpenApiRequest,
5+
OpenAPIV3
56
} from './framework/types';
67
import { defaultSerDes } from './framework/base.serdes';
78
import { AjvOptions } from './framework/ajvOptions';
8-
import { OpenAPIV3 } from './framework/types';
9-
109
export { OpenApiValidatorOpts } from './framework/types';
1110

1211
export class OpenApiValidator {
@@ -31,7 +30,7 @@ export class OpenApiValidator {
3130
this.ajvOpts = new AjvOptions(options);
3231
}
3332

34-
createValidator(): Function {
33+
createValidator(): (request: OpenApiRequest) => Promise<void> {
3534
const specAsync = this.loadSpec(this.options.apiSpec);
3635

3736
let requestValidator;
@@ -86,7 +85,7 @@ export class OpenApiValidator {
8685
return Object.assign(handler.schema);
8786
}
8887

89-
public async loadValidator(): Promise<Function> {
88+
public async loadValidator(): Promise<(request: OpenApiRequest) => Promise<void>> {
9089
const requestValidator = new RequestValidator(null, this.ajvOpts.request);
9190
await requestValidator.loadCompiled(this.options.compiledFilePath);
9291
return async (request: OpenApiRequest): Promise<void> => {
@@ -105,7 +104,7 @@ export class OpenApiValidator {
105104
options.serDes = defaultSerDes;
106105
} else {
107106
defaultSerDes.forEach(currentDefaultSerDes => {
108-
let defaultSerDesOverride = options.serDes.find(
107+
const defaultSerDesOverride = options.serDes.find(
109108
currentOptionSerDes => {
110109
return currentDefaultSerDes.format === currentOptionSerDes.format;
111110
}

src/middlewares/openapi.request.validator.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ export class RequestValidator {
191191
): OpenApiRequestHandler {
192192
const reqSchema = this.apiDoc && this.apiDoc.paths[path] && this.apiDoc.paths[path][method.toLowerCase()];
193193
if (!reqSchema) {
194-
return (): void => {};
194+
return (() => {});
195195
}
196196

197197
if (!this.ajv) {

src/middlewares/parsers/body.parse.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import {
66
} from '../../framework/types';
77

88
export class BodySchemaParser {
9-
constructor() {
10-
}
119
public parse(
1210
path: string,
1311
pathSchema: OpenAPIV3.OperationObject,

src/middlewares/parsers/schema.preprocessor.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Root<T> extends Node<T, T> {
3333
}
3434

3535
type SchemaObject = OpenAPIV3.SchemaObject;
36+
type NonArraySchemaObject = OpenAPIV3.NonArraySchemaObject;
3637
type SchemaObjectNode = Node<SchemaObject, SchemaObject>;
3738

3839
interface TopLevelPathNodes {
@@ -217,15 +218,15 @@ export class SchemaPreprocessor {
217218

218219
if (nschema) {
219220
// This null check should no longer be necessary
220-
this.handleSerDes(pschema, nschema);
221+
this.handleSerDes(pschema, nschema as NonArraySchemaObject);
221222
this.handleReadonly(pschema, nschema, options);
222223
}
223224
}
224225
}
225226

226227
private handleSerDes(
227228
parent: SchemaObject,
228-
schema: SchemaObject
229+
schema: NonArraySchemaObject
229230
) {
230231
if (
231232
schema.type === 'string'

0 commit comments

Comments
 (0)