From de70166aa8524575732f49472a2db9aa0a4c3b22 Mon Sep 17 00:00:00 2001 From: Arne Schubert Date: Wed, 2 May 2018 17:37:13 +0200 Subject: [PATCH] Add type definitions for express-jsonschema (#25471) * Add type definitions for express-jsonschema * Add TypeScript version comment in head --- .../express-jsonschema-tests.ts | 71 +++++++++++++++++++ types/express-jsonschema/index.d.ts | 54 ++++++++++++++ types/express-jsonschema/tsconfig.json | 23 ++++++ types/express-jsonschema/tslint.json | 1 + 4 files changed, 149 insertions(+) create mode 100644 types/express-jsonschema/express-jsonschema-tests.ts create mode 100644 types/express-jsonschema/index.d.ts create mode 100644 types/express-jsonschema/tsconfig.json create mode 100644 types/express-jsonschema/tslint.json diff --git a/types/express-jsonschema/express-jsonschema-tests.ts b/types/express-jsonschema/express-jsonschema-tests.ts new file mode 100644 index 0000000000..85a3852d75 --- /dev/null +++ b/types/express-jsonschema/express-jsonschema-tests.ts @@ -0,0 +1,71 @@ +import { + JsonSchemaCustomPropertyError, + JsonSchemaValidation, + addSchemaProperties, + validate, +} from "express-jsonschema"; + +import { Request, Response, NextFunction } from "express"; +import { JSONSchema4 } from "json-schema"; + +let jsonSchema: JSONSchema4; +let expressMiddleware: (req: Request, res: Response, next: NextFunction) => void; + +jsonSchema = { + id: 'foo', + $ref: 'foo/bar', + $schema: 'http://json-schema.org/schema#', + title: 'foo', + description: 'bar', + default: 42, + multipleOf: 3, + maximum: 4, + exclusiveMaximum: true, + minimum: 5, + exclusiveMinimum: false, + maxLength: 6, + minLength: 7, + pattern: 'baz', + additionalItems: true, + items: [ + { items: [{ minLength: 4 }] } + ], + maxItems: 4, + minItems: 5, + uniqueItems: true, + maxProperties: 10, + minProperties: 11, + required: ['foo', 'bar'], + additionalProperties: false, + definitions: { + foo: { type: 'string' } + }, + properties: { + bar: { type: 'boolean' } + }, + patternProperties: { + foo: { type: 'integer' } + }, + dependencies: { + baz: { type: 'integer' } + }, + enum: ['foo', 42], + type: 'string', + allOf: [{}], + anyOf: [{}], + oneOf: [{}], + not: {}, + extends: 'foo', + bar: 4, + format: 'date-time', +}; + +expressMiddleware = validate({ test: jsonSchema }, [ jsonSchema ]); +addSchemaProperties({ + test: () => "This is an error with message...", + conditional: (instance: any, schema: JSONSchema4) => { + if (!instance) { + return "An instance should be present"; + } + } +}); diff --git a/types/express-jsonschema/index.d.ts b/types/express-jsonschema/index.d.ts new file mode 100644 index 0000000000..f089e1033e --- /dev/null +++ b/types/express-jsonschema/index.d.ts @@ -0,0 +1,54 @@ +// Type definitions for express-jsonschema 1.1 +// Project: https://github.com/trainiac/express-jsonschema#readme +// Definitions by: Arne Schubert +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +import { Request, Response, NextFunction } from "express"; +import { JSONSchema4 } from "json-schema"; + +export class JsonSchemaCustomPropertyError { + name: string; + message: string; + constructor(propertyName: string); +} + +export class JsonSchemaValidation { + name: string; + message: string; + validations: {[requestProperty: string]: Array<{ value: any, property: string, messages: string[]}> }; + constructor(validations: { [requestProperty: string]: { instance: any, property: string, message: string }}); +} + +/** + * Updates customProperties with the newProperties param. Provides a way for client to extend JSON Schema validations. + * + * @param newProperties - An object where the keys are the + * names of the new schema properties and the values are the respective + * functions that implement the validation. + * @throws Client tries to override an existing JSON Schema property. + */ +export function addSchemaProperties(newProperties: { [attribute: string]: (instance: any, schema: JSONSchema4, options: any, ctx: any) => void | string }): void; + +/** + * Accepts an object where the keys are request properties and the + * values are their respective schemas. Optionally, you may provide + * dependency schemas that are referenced by your schemas using `$ref` + * (see https://www.npmjs.com/package/jsonschema#complex-example-with-split-schemas-and-references + * for more details). + * Returns a middleware function that validates the given + * request properties when a request is made. If there is any invalid + * data a JsonSchemaValidation instance is passed to the next middleware. + * If the data is valid the next middleware is called with no params. + * + * @param schemas - An object where the keys are request properties + * and the values are their respective schemas. + * @param schemaDependencies - A list of schemas on which + * schemas in `schemas` parameter are dependent. These will be added + * to the `jsonschema` validator. + * @returns A middleware function. + */ +export function validate( + schemas: { [requestProperty: string]: JSONSchema4 }, + schemaDependencies?: JSONSchema4[], +): (req: Request, res: Response, next: NextFunction) => void; diff --git a/types/express-jsonschema/tsconfig.json b/types/express-jsonschema/tsconfig.json new file mode 100644 index 0000000000..964ad62e02 --- /dev/null +++ b/types/express-jsonschema/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "express-jsonschema-tests.ts" + ] +} diff --git a/types/express-jsonschema/tslint.json b/types/express-jsonschema/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/express-jsonschema/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }