Add type definitions for express-jsonschema (#25471)

* Add type definitions for express-jsonschema

* Add TypeScript version comment in head
This commit is contained in:
Arne Schubert
2018-05-02 08:37:13 -07:00
committed by Andy
parent 3d4a2de3c4
commit de70166aa8
4 changed files with 149 additions and 0 deletions
@@ -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";
}
}
});
+54
View File
@@ -0,0 +1,54 @@
// Type definitions for express-jsonschema 1.1
// Project: https://github.com/trainiac/express-jsonschema#readme
// Definitions by: Arne Schubert <https://github.com/atd-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;
+23
View File
@@ -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"
]
}
+1
View File
@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }