Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keyword option overrride, unit test and readme update #153

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ Defaults:
{
keepErrors: false,
singleError: false,
keyword: 'errorMessage'
}
```

Expand All @@ -321,6 +322,7 @@ Defaults:
- `false` (default): create multiple errors, one for each message
- `true`: create single error, messages are concatenated using `"; "`
- non-empty string: this string is used as a separator to concatenate messages
- _keyword_: Override the keyword for error messages, for example x-error-message for use with swagger extensions

## Supporters

Expand Down
114 changes: 91 additions & 23 deletions spec/options.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,31 @@ import ajvErrors from ".."
import Ajv, {ErrorObject, SchemaObject, ValidateFunction} from "ajv"
import assert = require("assert")

function assertErrors(
validate: ValidateFunction,
expectedErrors: Partial<ErrorObject & {emUsed: boolean; errors: string[]}>[]
): void {
const {errors} = validate
assert.strictEqual(errors?.length, expectedErrors.length)

expectedErrors.forEach((expectedErr, i) => {
const err = errors[i] as ErrorObject & {emUsed: boolean}
assert.strictEqual(err.keyword, expectedErr.keyword)
assert.strictEqual(err.instancePath, expectedErr.instancePath)
assert.strictEqual(err.emUsed, expectedErr.emUsed)
if (expectedErr.keyword === "errorMessage") {
assert.strictEqual(err.params.errors.length, expectedErr.errors?.length)
expectedErr.errors?.forEach((matchedKeyword: string, j: number) =>
assert.strictEqual(err.params.errors[j].keyword, matchedKeyword)
)
}
})
}

describe("options", () => {
let ajv: Ajv
let ajv: Ajv

beforeEach(() => {
beforeEach(() => {
ajv = new Ajv({allErrors: true})
})

Expand Down Expand Up @@ -180,6 +201,74 @@ describe("options", () => {
})
})

describe("keyword option", () => {
it("should set the keyword", () => {
ajvErrors(ajv, {keyword: 'x-errorMessage'})
testKeywordErrors()
})
function testKeywordErrors(): void {
const errorMessageKeyword = "x-errorMessage"
const schema: SchemaObject = {
type: "number",
minimum: 2,
maximum: 10,
multipleOf: 2,
[errorMessageKeyword]: {
type: "should be number",
minimum: "should be >= 2",
maximum: "should be <= 10",
multipleOf: "should be multipleOf 2",
},
}
const validate = ajv.compile(schema)
assert.strictEqual(validate(11), false)

assertErrors(validate, [
{
"instancePath": "",
"schemaPath": "#/x-errorMessage",
"keyword": "x-errorMessage",
"params": {
"errors": [
{
"instancePath": "",
"schemaPath": "#/maximum",
"keyword": "maximum",
"params": {
"comparison": "<=",
"limit": 10
},
"message": "must be <= 10",
"emUsed": true
}
]
},
"message": "should be <= 10"
},
{
"instancePath": "",
"schemaPath": "#/x-errorMessage",
"keyword": "x-errorMessage",
"params": {
"errors": [
{
"instancePath": "",
"schemaPath": "#/multipleOf",
"keyword": "multipleOf",
"params": {
"multipleOf": 2
},
"message": "must be multiple of 2",
"emUsed": true
}
]
},
"message": "should be multipleOf 2"
}
])
}
})

describe("singleError", () => {
describe("= true", () => {
it("should generate a single error for all keywords", () => {
Expand Down Expand Up @@ -228,25 +317,4 @@ describe("options", () => {
])
}
})

function assertErrors(
validate: ValidateFunction,
expectedErrors: Partial<ErrorObject & {emUsed: boolean; errors: string[]}>[]
): void {
const {errors} = validate
assert.strictEqual(errors?.length, expectedErrors.length)

expectedErrors.forEach((expectedErr, i) => {
const err = errors[i] as ErrorObject & {emUsed: boolean}
assert.strictEqual(err.keyword, expectedErr.keyword)
assert.strictEqual(err.instancePath, expectedErr.instancePath)
assert.strictEqual(err.emUsed, expectedErr.emUsed)
if (expectedErr.keyword === "errorMessage") {
assert.strictEqual(err.params.errors.length, expectedErr.errors?.length)
expectedErr.errors?.forEach((matchedKeyword: string, j: number) =>
assert.strictEqual(err.params.errors[j].keyword, matchedKeyword)
)
}
})
}
})
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ interface ChildErrors {
items?: ErrorsMap<number>
}

const keyword = "errorMessage"

const used: Name = new Name("emUsed")

const KEYWORD_PROPERTY_PARAMS = {
Expand All @@ -36,13 +34,18 @@ const KEYWORD_PROPERTY_PARAMS = {
export interface ErrorMessageOptions {
keepErrors?: boolean
singleError?: boolean | string
keyword?: string
}

const INTERPOLATION = /\$\{[^}]+\}/
const INTERPOLATION_REPLACE = /\$\{([^}]+)\}/g
const EMPTY_STR = /^""\s*\+\s*|\s*\+\s*""$/g
let keyword = 'errorMessage'

function errorMessage(options: ErrorMessageOptions): CodeKeywordDefinition {
if(options.keyword){
keyword = options.keyword;
}
return {
keyword,
schemaType: ["string", "object"],
Expand Down