From 60bd0fc81e98b65a769e4a827efcc9363ba9cfe4 Mon Sep 17 00:00:00 2001 From: Emily Marigold Klassen Date: Tue, 5 Nov 2019 09:32:36 -0800 Subject: [PATCH] Add types for json-schema-compare (#40085) * Add types for json-schema-compare * Use JSONSchemaDefinition instead of only schema --- types/json-schema-compare/index.d.ts | 42 +++++++++++++++++++ .../json-schema-compare-tests.ts | 26 ++++++++++++ types/json-schema-compare/tsconfig.json | 23 ++++++++++ types/json-schema-compare/tslint.json | 1 + 4 files changed, 92 insertions(+) create mode 100644 types/json-schema-compare/index.d.ts create mode 100644 types/json-schema-compare/json-schema-compare-tests.ts create mode 100644 types/json-schema-compare/tsconfig.json create mode 100644 types/json-schema-compare/tslint.json diff --git a/types/json-schema-compare/index.d.ts b/types/json-schema-compare/index.d.ts new file mode 100644 index 0000000000..c1d7c5a7f5 --- /dev/null +++ b/types/json-schema-compare/index.d.ts @@ -0,0 +1,42 @@ +// Type definitions for json-schema-compare 0.2 +// Project: https://github.com/mokkabonna/json-schema-compare#readme +// Definitions by: Emily Marigold Klassen +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.8 + +import { JSONSchema4, JSONSchema6, JSONSchema7, JSONSchema6Definition, JSONSchema7Definition } from 'json-schema'; + +export = compare; + +type JSONSchemaComparee = JSONSchema4 | JSONSchema6Definition | JSONSchema7Definition | undefined; +type KnownKeys = { + [K in keyof T]: string extends K ? never : K +} extends { [_ in keyof T]: infer U } ? U : never; +/** + * The `string & {''?: never}` is a workaround for + * [Microsoft/TypeScript#29729](https://github.com/Microsoft/TypeScript/issues/29729). + * It will be removed as soon as it's not needed anymore. + */ +type JSONSchemaKeys = KnownKeys | keyof JSONSchema6 | keyof JSONSchema7 | string & {''?: never}; +interface Options { + /** + * Ignores certain keywords, useful to exclude meta keywords like title, + * description etc or custom keywords. If all you want to know if they are + * the same in terms of validation keywords. + * + * @default [] + */ + ignore?: JSONSchemaKeys[]; +} + +/** + * Compare json schemas correctly. + * + * - Ignores sort for arrays where sort does not matter, like required, enum, type, anyOf, oneOf, anyOf, dependencies (if array) + * - Compares correctly type when array or string + * - Ignores duplicate values before comparing + * - For schemas and sub schemas `undefined`, `true` and `{}` are equal + * - For minLength, minItems and minProperties `undefined` and `0` are equal + * - For uniqueItems, `undefined` and `false` are equal + */ +declare function compare(a: JSONSchemaComparee, b: JSONSchemaComparee, options?: Options): boolean; diff --git a/types/json-schema-compare/json-schema-compare-tests.ts b/types/json-schema-compare/json-schema-compare-tests.ts new file mode 100644 index 0000000000..9b9b4a3efb --- /dev/null +++ b/types/json-schema-compare/json-schema-compare-tests.ts @@ -0,0 +1,26 @@ +import compare = require('json-schema-compare'); + +// @ExpectType boolean +compare({ + title: 'title 1', + type: ['object'], + uniqueItems: false, + dependencies: { + name: ['age', 'lastName'] + }, + required: ['name', 'age', 'name'] +}, { + title: 'title 2', + type: 'object', + required: ['age', 'name'], + dependencies: { + name: ['lastName', 'age'] + }, + properties: { + name: { + minLength: 0 + } + } +}, { + ignore: ['title', 'customKey'] +}); diff --git a/types/json-schema-compare/tsconfig.json b/types/json-schema-compare/tsconfig.json new file mode 100644 index 0000000000..c95668ab7a --- /dev/null +++ b/types/json-schema-compare/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "json-schema-compare-tests.ts" + ] +} diff --git a/types/json-schema-compare/tslint.json b/types/json-schema-compare/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/json-schema-compare/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }