ajv-validator / ajv-validator/ajv-formats

Difficulty troubleshooting formatExclusiveMinimum error.

オープン
#32 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る
主要言語
TypeScript
スター
228
フォーク
42
PR マージ指標
30日以内にマージされた PR はありません

説明

Hi!, I'm trying to use `formatExclusiveMinimum` with $data to compare two date fields and validate that `endDate` > `startDate`, but I'm having trouble tracking down an error.

package.json `ajv` version snippet:
```
"ajv": "8.6.0",
"ajv-formats": "^2.1.0",
"ajv-keywords": "^5.0.0",
"ajv-merge-patch": "4.1.0"
```

`ajv` setup:
```javacsript
parameterAjv = new Ajv({
schemas,
strict: 'log',
$data: true,
removeAdditional: 'all',
useDefaults: false,
code: {
lines: true,
optimize: false
}
})

require('ajv-merge-patch/keywords/merge')(parameterAjv)

const addFormats = require('ajv-formats')
addFormats(parameterAjv, {
formats: ['date', 'time', 'date-time', 'uri', 'regex'],
keywords: true
})
schema = parameterAjv.getSchema(schemaPath)
```

An example from my schema:
``` javascript
{
"query": {
"type": "object",
"properties": {
"propertyId": {
"description": "The property you are querying",
"$ref": "Common.schema.json#/definitions/propertyId"
},
"startDate": {
"description": "The date check-in availability. In the property's local timezone.",
"type": "string",
"format": "date"
},
"endDate": {
"description": "The date check-out availability. In the property's local timezone.",
"type": "string",
"format": "date",
"formatExclusiveMinimum": {
"$data": "1/startDate"
}
}
},
"required": ["startDate", "endDate"]
}
}
```

When I pass in any combination of valid date strings for `startDate` and `endDate` I get the following error:

``` javascript
{
"instancePath": "/query/endDate",
"schemaPath": "#/properties/query/properties/endDate/formatExclusiveMinimum",
"keyword": "formatExclusiveMinimum",
"params": {
"_items": [
"{comparison: ",
"\">\"",
", limit: ",
"{\"str\":\"vSchema0\"}",
"}"
]
},
"message": {
"_items": ["\"should be > \"", "+", "{\"str\":\"vSchema0\"}"]
}
}
```

Is this error format correct?

I've been trying to track this down in the code generated by `ajv` to validate this field and I'm having trouble understanding one of the condition checks.

Here is a snippet of the generated code that deals with the `endDate` comparison to `startDate`:
``` javascript
if (valid1) {
if (data0.endDate !== undefined) {
let data8 = data0.endDate;
const _errs23 = errors;
if (errors === _errs23) {
if (errors === _errs23) {
if (typeof data8 === "string") {
if (!formats6.validate(data8)) {
validate242.errors = [
{
instancePath:
instancePath + "/query/endDate",
schemaPath:
"#/properties/query/properties/endDate/format",
keyword: "format",
params: { format: "date" },
message:
'must match format "' +
"date" +
'"',
},
];
return false;
} else {
const vSchema0 =
data0 && data0.startDate;
if (
vSchema0 !== undefined &&
(typeof vSchema0 !== "string" || { // Everything makes sense up until here
_items: [
"",
'{"str":"formats6","prefix":"formats","value":{"key":"date","ref":{},"code":{"_items":["","{\\"_items\\":[\\"require(\\\\\\"ajv-formats/dist/formats\\\\\\").\\",{\\"str\\":\\"fullFormats\\"},\\"\\"]}","",".date",""]}},"scopePath":{"_items":[".",{"str":"formats"},"[",1,"]"]}}',
".compare(",
'{"str":"data8"}',
", ",
'{"str":"vSchema0"}',
") ",
"<=",
" 0",
],
})
) {
validate242.errors = [
{
instancePath:
instancePath + "/query/endDate",
schemaPath:
"#/properties/query/properties/endDate/formatExclusiveMinimum",
keyword: "formatExclusiveMinimum",
params: {
_items: [
"{comparison: ",
'">"',
", limit: ",
'{"str":"vSchema0"}',
"}",
],
},
message: {
_items: [
'"should be > "',
"+",
'{"str":"vSchema0"}',
],
},
},
];
return false;
}
}
} else {
validate242.errors = [
{
instancePath:
instancePath + "/query/endDate",
schemaPath:
"#/properties/query/properties/endDate/type",
keyword: "type",
params: { type: "string" },
message: "must be string",
},
];
return false;
}
}
}
var valid1 = _errs23 === errors;
} else {
var valid1 = true;
}
```

Can anyone point me in the right direction? I've been back and forth over the configuration of `ajv` with `ajv-formats` but can't seem to make any progress.

コントリビューションガイド

このリポジトリのコントリビューションガイドは索引されていません

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。