Skip to content

Commit 4c6322c

Browse files
committed
[ajv8] Improve error path inference for propertyNames error
1 parent 35ff1a3 commit 4c6322c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

.changeset/plenty-bees-mate.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@sjsf/ajv8-validator": patch
3+
---
4+
5+
Improve error path inference for `propertyNames` error

mkfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ t:
1111
b:
1212
pnpm run build
1313

14-
v/:
14+
ajv/:
1515
pushd packages/ajv8-validator
1616
b:
1717
pnpm run build

packages/ajv8-validator/src/validator.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ import {
1313
type SchemaValue,
1414
} from "@sjsf/form/core";
1515
import {
16+
computePseudoId,
1617
DEFAULT_ID_PREFIX,
1718
DEFAULT_ID_SEPARATOR,
19+
DEFAULT_PSEUDO_ID_SEPARATOR,
1820
pathToId,
1921
type Config,
2022
type FieldErrors,
@@ -36,7 +38,8 @@ export class AjvValidator implements FormValidator<ErrorObject> {
3638
private readonly ajv: Ajv,
3739
private readonly uiSchema: UiSchemaRoot = {},
3840
private readonly idPrefix: string = DEFAULT_ID_PREFIX,
39-
private readonly idSeparator: string = DEFAULT_ID_SEPARATOR
41+
private readonly idSeparator: string = DEFAULT_ID_SEPARATOR,
42+
private readonly idPseudoSeparator: string = DEFAULT_PSEUDO_ID_SEPARATOR
4043
) {}
4144

4245
reset() {
@@ -148,13 +151,26 @@ export class AjvValidator implements FormValidator<ErrorObject> {
148151
}
149152

150153
private instancePathToId(
151-
{ params: { missingProperty } }: ErrorObject,
154+
{
155+
params: { missingProperty, propertyName: propertyNameParam },
156+
propertyName = propertyNameParam,
157+
}: ErrorObject,
152158
path: string[]
153159
) {
154-
const id = pathToId(this.idPrefix, this.idSeparator, path);
155-
return missingProperty !== undefined
156-
? `${id}${this.idSeparator}${missingProperty}`
157-
: id;
160+
let id = pathToId(this.idPrefix, this.idSeparator, path);
161+
id =
162+
missingProperty !== undefined
163+
? `${id}${this.idSeparator}${missingProperty}`
164+
: id;
165+
id =
166+
propertyName !== undefined
167+
? computePseudoId(
168+
this.idPseudoSeparator,
169+
`${id}${this.idSeparator}${propertyName}`,
170+
"key-input"
171+
)
172+
: id;
173+
return id;
158174
}
159175

160176
private errorObjectToMessage(

0 commit comments

Comments
 (0)