-
-
Notifications
You must be signed in to change notification settings - Fork 883
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: DefinedError as discriminated union with different parameter ty…
- Loading branch information
1 parent
61e7520
commit defca2c
Showing
20 changed files
with
181 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type {ErrorObject} from "../types" | ||
import type {RefErrorParams} from "./core/ref" | ||
import type {TypeErrorParams} from "../compile/validate/dataType" | ||
import type {AdditionalItemsErrorParams} from "./applicator/additionalItems" | ||
import type {AdditionalPropsErrorParams} from "./applicator/additionalProperties" | ||
import type {DependenciesErrorParams} from "./applicator/dependencies" | ||
import type {IfErrorParams} from "./applicator/if" | ||
import type {OneOfErrorParams} from "./applicator/oneOf" | ||
import type {PropertyNamesErrorParams} from "./applicator/propertyNames" | ||
import type {LimitErrorParams, LimitNumberErrorParams} from "./validation/limit" | ||
import type {MultipleOfErrorParams} from "./validation/multipleOf" | ||
import type {PatternErrorParams} from "./validation/pattern" | ||
import type {RequiredErrorParams} from "./validation/required" | ||
import type {UniqueItemsErrorParams} from "./validation/uniqueItems" | ||
import type {ConstErrorParams} from "./validation/const" | ||
import type {EnumErrorParams} from "./validation/enum" | ||
import type {FormatErrorParams} from "./format/format" | ||
|
||
type LimitKeyword = | ||
| "maxItems" | ||
| "minItems" | ||
| "minProperties" | ||
| "maxProperties" | ||
| "minLength" | ||
| "maxLength" | ||
|
||
type LimitNumberKeyword = "maximum" | "minimum" | "exclusiveMaximum" | "exclusiveMinimum" | ||
|
||
type RefError = ErrorObject<"ref", RefErrorParams> | ||
type TypeError = ErrorObject<"type", TypeErrorParams> | ||
type ErrorWithoutParams = ErrorObject< | ||
"anyOf" | "contains" | "not" | "false schema", | ||
Record<string, never> | ||
> | ||
type AdditionalItemsError = ErrorObject<"additionalItems", AdditionalItemsErrorParams> | ||
type AdditionalPropsError = ErrorObject<"additionalProperties", AdditionalPropsErrorParams> | ||
type DependenciesError = ErrorObject<"dependencies", DependenciesErrorParams> | ||
type IfKeywordError = ErrorObject<"if", IfErrorParams> | ||
type OneOfError = ErrorObject<"oneOf", OneOfErrorParams> | ||
type PropertyNamesError = ErrorObject<"propertyNames", PropertyNamesErrorParams> | ||
type LimitError = ErrorObject<LimitKeyword, LimitErrorParams> | ||
type LimitNumberError = ErrorObject<LimitNumberKeyword, LimitNumberErrorParams> | ||
type MultipleOfError = ErrorObject<"multipleOf", MultipleOfErrorParams> | ||
type PatternError = ErrorObject<"pattern", PatternErrorParams> | ||
type RequiredError = ErrorObject<"required", RequiredErrorParams> | ||
type UniqueItemsError = ErrorObject<"uniqueItems", UniqueItemsErrorParams> | ||
type ConstError = ErrorObject<"const", ConstErrorParams> | ||
type EnumError = ErrorObject<"enum", EnumErrorParams> | ||
type FormatError = ErrorObject<"format", FormatErrorParams> | ||
|
||
export type DefinedError = | ||
| RefError | ||
| TypeError | ||
| ErrorWithoutParams | ||
| AdditionalItemsError | ||
| AdditionalPropsError | ||
| DependenciesError | ||
| IfKeywordError | ||
| OneOfError | ||
| PropertyNamesError | ||
| LimitNumberError | ||
| MultipleOfError | ||
| LimitError | ||
| PatternError | ||
| RequiredError | ||
| UniqueItemsError | ||
| ConstError | ||
| EnumError | ||
| FormatError |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {DefinedError} from "../../dist/types" | ||
import _Ajv from "../ajv" | ||
import chai from "../chai" | ||
const should = chai.should() | ||
|
||
describe("error object parameters type", () => { | ||
const ajv = new _Ajv({allErrors: true}) | ||
|
||
it("should be determined by the keyword", () => { | ||
const validate = ajv.compile({minimum: 0, multipleOf: 2}) | ||
const valid = validate(-1) | ||
valid.should.equal(false) | ||
const errs = validate.errors | ||
if (errs) { | ||
errs.length.should.equal(2) | ||
for (const err of errs as DefinedError[]) { | ||
switch (err.keyword) { | ||
case "minimum": | ||
err.params.limit.should.equal(0) | ||
err.params.comparison.should.equal(">=") | ||
break | ||
case "multipleOf": | ||
err.params.multipleOf.should.equal(2) | ||
break | ||
default: | ||
should.fail() | ||
} | ||
} | ||
} | ||
}) | ||
}) |