Skip to content

Commit 8a999b3

Browse files
author
Sascha Goldhofer
committed
types: update core member and utility typings
1 parent cf7284d commit 8a999b3

File tree

9 files changed

+44
-29
lines changed

9 files changed

+44
-29
lines changed

lib/addValidator.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Core from "./cores/CoreInterface";
22
import { JSONValidator } from "./types";
3+
import { CreateError } from "./utils/createCustomError";
34

45

56
/**
@@ -10,7 +11,7 @@ import { JSONValidator } from "./types";
1011
* @param errorId id of error @see /lib/validation/errors
1112
* @param errorCreator - function returning an error-object @see /lib/utils/createCustomError
1213
*/
13-
function addError(core: Core, errorId: string, errorCreator) {
14+
function addError(core: Core, errorId: string, errorCreator: CreateError) {
1415
if (typeof errorCreator !== "function") {
1516
throw new Error(`Error callback 'errorCreator' must be of type function. Received '${typeof errorCreator}'`);
1617
}
@@ -28,11 +29,10 @@ function addFormat(core: Core, formatType: string, validationFunction: JSONValid
2829
if (typeof validationFunction !== "function") {
2930
throw new Error(`Validation function expected. Received ${typeof validationFunction}`);
3031
}
31-
if (core.validateFormat[formatType] == null) {
32-
core.validateFormat[formatType] = validationFunction;
33-
return;
32+
if (core.validateFormat[formatType]) {
33+
throw new Error(`A format '${formatType}' is already registered to validation`);
3434
}
35-
throw new Error(`A format '${formatType}' is already registered to validation`);
35+
core.validateFormat[formatType] = validationFunction;
3636
}
3737

3838

lib/compile/getRef.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { JSONSchema } from "../types";
55

66

77
const suffixes = /(#|\/)+$/g;
8-
const isObject = val => (getTypeOf(val) === "object");
8+
const isObject = (val: unknown): val is Record<string, any> => (getTypeOf(val) === "object");
99

1010

1111
// 1. combined is known

lib/cores/CoreInterface.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,24 @@ import resolveRef from "../resolveRef.withOverwrite";
33
import compileSchema from "../compileSchema";
44
import resolveAnyOf from "../resolveAnyOf";
55
import resolveAllOf from "../resolveAllOf";
6-
import { JSONSchema, JSONPointer, JSONError } from "../types";
6+
import { JSONSchema, JSONPointer, JSONValidator, JSONTypeValidator, JSONError } from "../types";
7+
import { CreateError } from "../utils/createCustomError";
78

89

910
/* eslint no-unused-vars: 0 no-empty-function: 0 */
1011
export default class CoreInterface {
12+
/** entry point of schema */
1113
__rootSchema: JSONSchema;
12-
errors;
13-
typeKeywords;
14-
validateFormat;
15-
validateKeyword;
16-
validateType;
14+
/** error creators by id */
15+
errors: Record<string, CreateError> = {};
16+
/** map for valid keywords of a type */
17+
typeKeywords: Record<string, string[]> = {};
18+
/** keyword validators */
19+
validateKeyword: Record<string, JSONValidator> = {};
20+
/** type validators */
21+
validateType: Record<string, JSONTypeValidator> = {};
22+
/** format validators */
23+
validateFormat: Record<string, JSONValidator> = {};
1724

1825
constructor(schema?: JSONSchema) {
1926
this.setSchema(schema);

lib/cores/Draft07.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,31 @@ import resolveOneOf from "../resolveOneOf.strict";
55
import resolveRef from "../resolveRef.strict";
66
import getTemplate from "../getTemplate";
77
import getSchema from "../getSchema";
8+
import addSchema from "../addSchema";
89
import each from "../each";
910
import compileSchema from "../draft06/compile";
1011
import { JSONSchema, JSONPointer } from "../types";
11-
12-
import remotes from "../../remotes";
1312
import draft07 from "../../remotes/draft07.json";
1413

15-
// @ts-ignore
16-
remotes["http://json-schema.org/draft-07/schema"] = compileSchema(draft07);
17-
1814
import TYPE_KEYWORD_MAPPING from "../draft06/validation/typeKeywordMapping";
1915
import KEYWORDS from "../draft06/validation/keyword";
2016
import TYPES from "../draft06/validation/type";
2117
import FORMATS from "../validation/format";
2218
import ERRORS from "../validation/errors";
19+
import addValidator from "../addValidator";
20+
21+
addSchema("http://json-schema.org/draft-07/schema", draft07)
2322

2423

2524
export default class Draft07Core extends CoreInterface {
2625

2726
constructor(schema?: JSONSchema) {
2827
super(schema);
2928
this.typeKeywords = JSON.parse(JSON.stringify(TYPE_KEYWORD_MAPPING));
30-
this.validateKeyword = Object.assign({}, KEYWORDS);
31-
this.validateType = Object.assign({}, TYPES);
32-
this.validateFormat = Object.assign({}, FORMATS);
33-
this.errors = Object.assign({}, ERRORS);
29+
Object.assign(this.validateKeyword, KEYWORDS);
30+
Object.assign(this.validateType, TYPES);
31+
Object.keys(FORMATS).forEach(id => addValidator.format(this, id, FORMATS[id]))
32+
Object.keys(ERRORS).forEach(id => addValidator.error(this, id, ERRORS[id]))
3433
}
3534

3635
get rootSchema() {

lib/getTypeOf.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
const toString = Object.prototype.toString;
22

3+
export type JSType = "array"|"bigint"|"boolean"|"function"|"null"|"number"|"object"|"string"|"symbol"|"undefined";
34

4-
export default function getTypeOf(value: any) {
5+
export default function getTypeOf(value: unknown) : JSType {
56
// eslint-disable-next-line newline-per-chained-call
67
return toString.call(value).match(/\s([^\]]+)\]/).pop().toLowerCase();
78
}

lib/types.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,9 @@ export type JSONError = {
1515
}
1616

1717
export interface JSONValidator {
18-
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): void|undefined|JSONError|Array<JSONError>;
18+
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): void|undefined|JSONError|JSONError[];
19+
}
20+
21+
export interface JSONTypeValidator {
22+
(core: Core, schema: JSONSchema, value: any, pointer: JSONPointer): Array<void|undefined|JSONError|JSONError[]>;
1923
}

lib/validate.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
1-
import getTypeOf from "./getTypeOf";
1+
import getTypeOf, { JSType } from "./getTypeOf";
22
import { errorOrPromise } from "./utils/filter";
33
import flattenArray from "./utils/flattenArray";
44
import { JSONSchema, JSONPointer, JSONError } from "./types";
55
import Core from "./cores/CoreInterface";
66
import equal from "fast-deep-equal";
77

88

9-
function getJsonSchemaType(value, expectedType) {
10-
let jsType = getTypeOf(value);
11-
9+
function getJsonSchemaType(value, expectedType): JSType|"integer" {
10+
const jsType = getTypeOf(value);
1211
if (
1312
jsType === "number" && (expectedType === "integer" ||
1413
(Array.isArray(expectedType) && expectedType.includes("integer")))
1514
) {
16-
jsType = Number.isInteger(value) ? "integer" : "number";
15+
return Number.isInteger(value) ? "integer" : "number";
1716
}
1817
return jsType;
1918
}

lib/validation/type.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
import { JSONTypeValidator } from "../types";
2+
13
/**
24
* @todo: type is also a keyword, as is properties, items, etc
35
*
46
* An instance has one of six primitive types (http://json-schema.org/latest/json-schema-core.html#rfc.section.4.2)
57
* or seven in case of ajv https://github.com/epoberezkin/ajv/blob/master/KEYWORDS.md#type
68
* 1 null, 2 boolean, 3 object, 4 array, 5 number, 6 string (7 integer)
79
*/
8-
export default {
10+
const typeValidators: Record<string, JSONTypeValidator> = {
911

1012
array: (core, schema, value, pointer) =>
1113
core.typeKeywords.array
@@ -42,3 +44,5 @@ export default {
4244
.filter(key => schema && schema[key] != null)
4345
.map(key => core.validateKeyword[key](core, schema, value, pointer))
4446
};
47+
48+
export default typeValidators;

test/unit/addValidator.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ describe("addValidator", () => {
2121
it("should overwrite 'minLengthError'", () => {
2222
addValidator.error(core, "minLengthError", data => ({
2323
type: "error",
24+
name: "minLengthError",
2425
code: "custom-min-length-error",
2526
message: "my custom error message",
2627
data

0 commit comments

Comments
 (0)