Skip to content
This repository was archived by the owner on Sep 8, 2020. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ build/Release
node_modules

.DS_Store

package-lock.json
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
language: node_js
node_js:
- "4"
- "6"
- 10
- 12
- 14
after_script:
- coveralls < coverage/lcov.info
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function afterEachFunc(result) {

// Pass option log == false to prevent default error logging

// If result.passed is false the test will fails after this function returns
// If result.passed is false the test will fail after this function returns
}
```

Expand Down
79 changes: 79 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
declare function jsonSchemaTest(
validators: jsonSchemaTest.Validator[],
opts: jsonSchemaTest.Options
): void;

declare namespace jsonSchemaTest {
type ValidationError = any;

type Schema = Record<string, unknown> | boolean;

interface Validator {
validate(
schema: Schema,
data: unknown
): boolean | Promise<boolean | unknown>;
errors?: ValidationError[] | null;
}

interface Options {
description?: string;
suites: Record<string, Suites>;
async?: boolean;
asyncValid?: "data";
afterEach?: (res: TestResult) => void;
afterError?: (res: TestResult) => void; // res.passed === false
log?: boolean; // pass false to prevent logging
only?: true | string[]; // names of TestSuite or filenames or true to perform only these tests
skip?: true | string[]; // skip all or some tests
cwd?: string; // working dir, pass __dirname to use paths relative to the module
hideFolder?: string;
timeout?: number;
assert?: Assert;
}

type Suites = SuitesPath | TestSuite[] | TestSuitePath[];

type SuitesPath = string; // glob pattern

interface TestSuite {
name: string;
test: TestGroup[];
}

interface TestSuitePath {
name: string;
path: string;
}

interface TestGroup {
description: string;
schema?: Schema | string;
schemas?: (Schema | string)[];
tests: Test[];
}

interface Test {
description: string;
data: unknown;
valid?: boolean;
error?: string;
}

interface TestResult {
validator: Validator;
schema: Schema;
data: unknown;
valid: boolean; // validation result
expected: boolean; // expected validation result
errors: ValidationError[] | null; // validation errors if valid === false
passed: boolean; // true if valid == expected
}

interface Assert {
(ok: boolean): void;
equal: (x: unknown, y: unknown) => void;
}
}

export = jsonSchemaTest;
15 changes: 8 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"name": "json-schema-test",
"version": "2.0.0",
"version": "2.1.0",
"description": "Testing JSON schemas against sample data",
"main": "index.js",
"scripts": {
"test-pass": "mocha spec/*.pass.js -R spec",
"test-fail": "mocha spec/*.fail.js -R spec | grep ' 0 passing' | grep -q '0'",
"test-spec": "npm run test-pass && npm run test-fail",
"test": "nyc npm run test-spec"
"test": "nyc npm run test-spec && tsc index.d.ts"
},
"repository": {
"type": "git",
Expand All @@ -28,11 +28,12 @@
"glob": "^5.0.10"
},
"devDependencies": {
"ajv": "^4.9.0",
"coveralls": "^2.11.15",
"mocha": "^3.2.0",
"nyc": "^10.0.0",
"pre-commit": "^1.1.3"
"ajv": "^6.12.4",
"coveralls": "^3.0.1",
"mocha": "^8.0.1",
"nyc": "^15.0.0",
"pre-commit": "^1.1.3",
"typescript": "^4.0.2"
},
"nyc": {
"exclude": [
Expand Down