Skip to content

Commit d62441f

Browse files
committed
feat(@angular-devkit/core): provide prompt validation errors to provider
This change provides the first schema validation error to a prompt provider when using a prompt definition's `validate` function. This allows a prompt provider to show additional context to a user when an entered value is invalid.
1 parent 5fdc0a6 commit d62441f

File tree

1 file changed

+15
-3
lines changed
  • packages/angular_devkit/core/src/json/schema

1 file changed

+15
-3
lines changed

packages/angular_devkit/core/src/json/schema/registry.ts

+15-3
Original file line numberDiff line numberDiff line change
@@ -599,10 +599,22 @@ export class CoreSchemaRegistry implements SchemaRegistry {
599599
: (parentSchema as JsonObject).default as string[],
600600
async validator(data: JsonValue) {
601601
try {
602-
return await it.self.validate(parentSchema, data);
603-
} catch {
604-
return false;
602+
const result = await it.self.validate(parentSchema, data);
603+
// If the schema is sync then false will be returned on validation failure
604+
if (result) {
605+
return result;
606+
} else if (it.self.errors?.length) {
607+
// Validation errors will be present on the Ajv instance when sync
608+
return it.self.errors[0].message;
609+
}
610+
} catch (e) {
611+
// If the schema is async then an error will be thrown on validation failure
612+
if (Array.isArray(e.errors) && e.errors.length) {
613+
return e.errors[0].message;
614+
}
605615
}
616+
617+
return false;
606618
},
607619
};
608620

0 commit comments

Comments
 (0)