feat: enforce typed page schema in AI drafts
This commit is contained in:
@@ -65,8 +65,12 @@ public final class TypedPageSchemaValidator {
|
||||
Set<String> usedComponents, Set<String> usedActions) {
|
||||
String componentType = node.path("type").asText("");
|
||||
if (!componentType.isBlank()) {
|
||||
usedComponents.add(componentType);
|
||||
if (!registry.supportsComponent(componentType)) {
|
||||
boolean registeredComponent = registry.supportsComponent(componentType);
|
||||
boolean componentCandidate = registeredComponent || isComponentCandidate(path, node);
|
||||
if (componentCandidate) {
|
||||
usedComponents.add(componentType);
|
||||
}
|
||||
if (componentCandidate && !registeredComponent) {
|
||||
issues.add(new TypedPageSchemaIssue(path + ".type", "UNSUPPORTED_COMPONENT",
|
||||
"组件未注册: " + componentType));
|
||||
}
|
||||
@@ -84,6 +88,27 @@ public final class TypedPageSchemaValidator {
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isComponentCandidate(String path, JsonNode node) {
|
||||
return "$".equals(path)
|
||||
|| path.endsWith(".body")
|
||||
|| path.contains(".body[")
|
||||
|| path.contains(".columns[")
|
||||
|| path.contains(".buttons[")
|
||||
|| path.contains(".headerToolbar[")
|
||||
|| path.contains(".footerToolbar[")
|
||||
|| path.contains(".tabs[")
|
||||
|| path.contains(".items[")
|
||||
|| node.has("actionType")
|
||||
|| node.has("body")
|
||||
|| node.has("columns")
|
||||
|| node.has("buttons")
|
||||
|| node.has("label")
|
||||
|| node.has("tpl")
|
||||
|| node.has("dialog")
|
||||
|| node.has("drawer")
|
||||
|| node.has("required");
|
||||
}
|
||||
|
||||
private TypedPageSchemaReport report(List<TypedPageSchemaIssue> issues, Set<String> usedComponents,
|
||||
Set<String> usedActions) {
|
||||
return new TypedPageSchemaReport(PageComponentRegistry.VERSION, issues.isEmpty(), List.copyOf(issues),
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.Map;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DataAccessException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -73,6 +74,7 @@ public class PageSchemaController {
|
||||
private final PageComponentRegistry componentRegistry;
|
||||
private final TypedPageSchemaValidator typedSchemaValidator;
|
||||
|
||||
@Autowired
|
||||
public PageSchemaController(JdbcTemplate jdbcTemplate) {
|
||||
this(jdbcTemplate, PageComponentRegistry.defaults());
|
||||
}
|
||||
|
||||
@@ -45,6 +45,23 @@ class TypedPageSchemaValidatorTest {
|
||||
assertTrue(report.issues().get(0).path().contains("$.body.type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void ignoresNonComponentTypeFieldsInConfigurationObjects() {
|
||||
TypedPageSchemaReport report = validator.validate("""
|
||||
{
|
||||
"type": "page",
|
||||
"body": {
|
||||
"type": "crud",
|
||||
"api": {"type": "json", "method": "get", "url": "/api/entity/EIP4AI_DEMO/list"}
|
||||
}
|
||||
}
|
||||
""");
|
||||
|
||||
assertTrue(report.valid());
|
||||
assertTrue(report.usedComponents().contains("crud"));
|
||||
assertFalse(report.usedComponents().contains("json"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void rejectsUnsupportedActionForComponent() {
|
||||
TypedPageSchemaReport report = validator.validate("""
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.eip4.platform.metadata.web;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.jdbc.core.JdbcTemplate;
|
||||
|
||||
class PageSchemaControllerWiringTest {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
.withBean(JdbcTemplate.class, () -> mock(JdbcTemplate.class))
|
||||
.withUserConfiguration(PageSchemaController.class);
|
||||
|
||||
@Test
|
||||
void springUsesJdbcTemplateConstructor() {
|
||||
contextRunner.run(context -> assertThat(context).hasSingleBean(PageSchemaController.class));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user