feat(schema): data_type 增 uri(绝对 URI 写入校验+xsd:anyURI 导出+AI/MCP 词表)
Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
c5826df832
commit
5dd86bfa95
10 files changed
+213
-4
No files matched your search
+1
-1
@@ -208,7 +208,7 @@ ontology-platform 把所有数据建模为"对象 + 属性 + 链接 + 动作 +
|
||||
|
||||
# 字段值约束(重要)
|
||||
|
||||
- data_type 取值:string / integer / number / boolean / date / datetime / enum / json / text / geoshape / geopoint / media / timeseries / timestamp
|
||||
- data_type 取值:string / integer / number / boolean / date / datetime / enum / json / text / geoshape / geopoint / media / timeseries / timestamp / uri
|
||||
- cardinality 取值:one_to_one / one_to_many / many_to_many(默认 many_to_many)
|
||||
- impl_kind 取值:builtin / script / external_api
|
||||
- protocol 取值:rest / mcp
|
||||
|
||||
@@ -108,12 +108,13 @@ func (a *app) classConfigOf(ctx context.Context, typ string) classConfig {
|
||||
}
|
||||
|
||||
// invalidateClassCache 类型/属性定义变更后调用。
|
||||
// 同时清 classCache / enumCache / axiomsCache 三层(共用 classMu)。
|
||||
// 同时清 classCache / enumCache / axiomsCache / uriCache 四层(共用 classMu)。
|
||||
func invalidateClassCache() {
|
||||
classMu.Lock()
|
||||
classAt = time.Time{}
|
||||
enumAt = time.Time{}
|
||||
axiomsAt = time.Time{}
|
||||
uriAt = time.Time{}
|
||||
classMu.Unlock()
|
||||
}
|
||||
|
||||
|
||||
@@ -786,3 +786,35 @@ func TestIntegration_DisplayNameLang(t *testing.T) {
|
||||
t.Errorf("导出缺 @zh 字面量: %s", data)
|
||||
}
|
||||
}
|
||||
|
||||
// uri data_type:写入校验 + xsd:anyURI 导出。
|
||||
// 注:创建实例统一走 POST /api/v1/objects(type 在 body),无 /objects/{type} 创建路由。
|
||||
func TestIntegration_URIDataType(t *testing.T) {
|
||||
env, _ := setupTestEnv(t)
|
||||
h := map[string]string{"X-Principal": "manager"}
|
||||
key := env.typeKey
|
||||
t.Cleanup(func() { env.httpDo(t, "DELETE", "/api/v1/ontology/object-types/"+key, nil, h) })
|
||||
env.httpDo(t, "POST", "/api/v1/ontology/object-types",
|
||||
map[string]any{"api_name": key, "display_name": "URI",
|
||||
"properties": []map[string]any{{"api_name": "homepage", "display_name": "主页", "data_type": "uri"}}}, h)
|
||||
|
||||
// 非法值 → 400
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/objects",
|
||||
map[string]any{"type": key, "pk": "x", "properties": map[string]any{"homepage": "not a uri"}}, h); code != 400 {
|
||||
t.Errorf("非绝对 URI 应 400, got %d", code)
|
||||
}
|
||||
// 合法 http → 201
|
||||
if code, _ := env.httpDo(t, "POST", "/api/v1/objects",
|
||||
map[string]any{"type": key, "pk": "x", "properties": map[string]any{"homepage": "https://example.org/x"}}, h); code != 201 && code != 200 {
|
||||
t.Errorf("合法 URI 应通过, got %d", code)
|
||||
}
|
||||
// 导出 xsd:anyURI
|
||||
req, _ := http.NewRequest("GET", env.srv.URL+"/api/v1/ontology/export?format=turtle", nil)
|
||||
req.Header.Set("X-Principal", "manager")
|
||||
resp, _ := http.DefaultClient.Do(req)
|
||||
defer resp.Body.Close()
|
||||
data, _ := io.ReadAll(resp.Body)
|
||||
if !strings.Contains(string(data), "xsd:anyURI") {
|
||||
t.Errorf("导出缺 xsd:anyURI: %s", data)
|
||||
}
|
||||
}
|
||||
@@ -241,7 +241,7 @@ func mcpToolList() []MCPTool {
|
||||
"api_name": map[string]interface{}{"type": "string", "description": "对象类型 api_name"},
|
||||
"property_api_name": map[string]interface{}{"type": "string", "description": "新属性的稳定标识(必填)"},
|
||||
"display_name": map[string]interface{}{"type": "string", "description": "新属性的中文显示名(必填)"},
|
||||
"data_type": map[string]interface{}{"type": "string", "description": "string/number/boolean/date/enum/...(默认 string)"},
|
||||
"data_type": map[string]interface{}{"type": "string", "description": "string/number/boolean/date/enum/uri/...(默认 string)"},
|
||||
"required": map[string]interface{}{"type": "boolean"},
|
||||
"enum_values": map[string]interface{}{"type": "array", "items": map[string]interface{}{"type": "string"}, "description": "enum 约束,如 [L0,L1,L2,L3]"},
|
||||
"description": map[string]interface{}{"type": "string"},
|
||||
|
||||
@@ -283,6 +283,11 @@ func (a *app) createObject(c *gin.Context) {
|
||||
"hint": "validate_axioms=false 可豁免(同 enum_violation 模式)"})
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "uri_violation") {
|
||||
c.JSON(400, gin.H{"error": "uri_violation", "detail": err.Error(),
|
||||
"hint": "值需为绝对 URI(http/https/mailto/urn 等)"})
|
||||
return
|
||||
}
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
@@ -352,6 +357,11 @@ func (a *app) updateObject(c *gin.Context) {
|
||||
"hint": "validate_axioms=false 可豁免(同 enum_violation 模式)"})
|
||||
return
|
||||
}
|
||||
if strings.HasPrefix(err.Error(), "uri_violation") {
|
||||
c.JSON(400, gin.H{"error": "uri_violation", "detail": err.Error(),
|
||||
"hint": "值需为绝对 URI(http/https/mailto/urn 等)"})
|
||||
return
|
||||
}
|
||||
c.JSON(400, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
+1
-1
@@ -42,7 +42,7 @@ type Property struct {
|
||||
ObjectID int64 `json:"object_type_id"`
|
||||
APIName string `json:"api_name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
DataType string `json:"data_type"` // string/integer/number/boolean/date/datetime/enum/json/text
|
||||
DataType string `json:"data_type"` // string/integer/number/boolean/date/datetime/enum/json/text/uri
|
||||
Required bool `json:"required"`
|
||||
EnumValues []string `json:"enum_values,omitempty"`
|
||||
Description string `json:"description"`
|
||||
|
||||
@@ -108,6 +108,12 @@ func applyObjectUpsert(ctx context.Context, db dbExec, a *app, in objectUpsert)
|
||||
}
|
||||
}
|
||||
|
||||
// URI 数据类型写入校验(GB/T 48000.3-2026 附录 A.3):作用于本次写入值 in.Properties,
|
||||
// 与 enum 同策略(部分写只校验本次带的键)。无 validate_uri 后门——uri 是新 opt-in 类型。
|
||||
if err := a.validateURIProps(ctx, in.Type, in.Properties); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// 公理写入校验(GB/T 48000.3-2026 §8.2):dateOrder 等规则作用于最终属性值。
|
||||
// 校验 newProps 而非 in.Properties,让 merge / replace 两条路径都按落地值走。
|
||||
if !in.SkipAxiomCheck {
|
||||
|
||||
@@ -52,6 +52,8 @@ func turtleRange(dataType string) string {
|
||||
return "xsd:date"
|
||||
case "datetime":
|
||||
return "xsd:dateTime"
|
||||
case "uri":
|
||||
return "xsd:anyURI"
|
||||
default: // string / text / enum / json / geoshape / ...
|
||||
return "xsd:string"
|
||||
}
|
||||
|
||||
@@ -137,3 +137,24 @@ func TestValidLangTag(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestTurtleRangeURI(t *testing.T) {
|
||||
if got := turtleRange("uri"); got != "xsd:anyURI" {
|
||||
t.Errorf("turtleRange(uri)=%q want xsd:anyURI", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsAbsURI(t *testing.T) {
|
||||
ok := []string{"https://example.org/x", "http://x.cn/a?b=c", "mailto:[email protected]", "urn:iso:std:48000.3"}
|
||||
bad := []string{"", "not a uri", "example.org/x", "/local", "://empty", "ht tp://x"}
|
||||
for _, s := range ok {
|
||||
if !isAbsURI(s) {
|
||||
t.Errorf("isAbsURI(%q)=false want true", s)
|
||||
}
|
||||
}
|
||||
for _, s := range bad {
|
||||
if isAbsURI(s) {
|
||||
t.Errorf("isAbsURI(%q)=true want false", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/url"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// URI 数据类型(GB/T 48000.3-2026 附录 A.3):写入校验绝对 URI;xsd:anyURI 导出;
|
||||
// 与 enum 校验同面 applyObjectUpsert、同缓存模式(classMu + TTL)。
|
||||
|
||||
var (
|
||||
uriMu = classMu
|
||||
uriCache map[string]map[string]bool
|
||||
uriAt time.Time
|
||||
)
|
||||
|
||||
// loadURIProps 类型 → 闭包合并后 uri 属性集。
|
||||
// 闭包合并策略:查 parent 关系 → 逐类型 typeClosure 根→自身 overlay(子覆盖父同名)。
|
||||
func (a *app) loadURIProps(ctx context.Context) map[string]map[string]bool {
|
||||
uriMu.RLock()
|
||||
if time.Since(uriAt) < classCacheTTL && uriCache != nil {
|
||||
m := uriCache
|
||||
uriMu.RUnlock()
|
||||
return m
|
||||
}
|
||||
uriMu.RUnlock()
|
||||
|
||||
out := map[string]map[string]bool{}
|
||||
if rows, err := a.pool.Query(ctx,
|
||||
`SELECT ot.api_name, p.api_name FROM properties p
|
||||
JOIN object_types ot ON ot.id=p.object_type_id
|
||||
WHERE p.data_type='uri'`); err == nil {
|
||||
for rows.Next() {
|
||||
var t, p string
|
||||
if err := rows.Scan(&t, &p); err != nil {
|
||||
continue
|
||||
}
|
||||
if out[t] == nil {
|
||||
out[t] = map[string]bool{}
|
||||
}
|
||||
out[t][p] = true
|
||||
}
|
||||
rows.Close()
|
||||
}
|
||||
// parentOf 单独一段拉全量父子对:「有父但自身无 uri 属性」的子类型同样要进合并循环,
|
||||
// 否则继承不到父 uri 校验(与 loadEnumMap 第二段同策略)。
|
||||
parentOf := map[string]string{}
|
||||
if prow, err := a.pool.Query(ctx,
|
||||
`SELECT ot.api_name, pa.api_name
|
||||
FROM object_types ot JOIN object_types pa ON pa.id = ot.parent_id`); err == nil {
|
||||
for prow.Next() {
|
||||
var t, pa string
|
||||
if err := prow.Scan(&t, &pa); err == nil {
|
||||
parentOf[t] = pa
|
||||
}
|
||||
}
|
||||
prow.Close()
|
||||
}
|
||||
for t := range parentOf {
|
||||
for _, anc := range a.typeClosure(ctx, t) {
|
||||
if anc == t {
|
||||
continue
|
||||
}
|
||||
if oa := out[anc]; oa != nil {
|
||||
if out[t] == nil {
|
||||
out[t] = map[string]bool{}
|
||||
}
|
||||
for k, v := range oa {
|
||||
out[t][k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
uriMu.Lock()
|
||||
uriCache, uriAt = out, time.Now()
|
||||
uriMu.Unlock()
|
||||
return out
|
||||
}
|
||||
|
||||
// isAbsURI 绝对 URI:scheme 非空 + (host 非空 或 opaque 非空)。支持 http/https/mailto/urn 等。
|
||||
func isAbsURI(s string) bool {
|
||||
s = strings.TrimSpace(s)
|
||||
if s == "" {
|
||||
return false
|
||||
}
|
||||
u, err := url.Parse(s)
|
||||
if err != nil || u.Scheme == "" {
|
||||
return false
|
||||
}
|
||||
return u.Host != "" || u.Opaque != ""
|
||||
}
|
||||
|
||||
// validateURIProps uri 属性写入校验:值缺省/空放行(required 另管);非字符串或非绝对 URI 即违例。
|
||||
// 无 validate_uri 后门——uri 是新 opt-in 类型,无存量负担(与 functionalLinks 同策略)。
|
||||
func (a *app) validateURIProps(ctx context.Context, typ string, props map[string]interface{}) error {
|
||||
if len(props) == 0 {
|
||||
return nil
|
||||
}
|
||||
mp := a.loadURIProps(ctx)[typ]
|
||||
if len(mp) == 0 {
|
||||
return nil
|
||||
}
|
||||
var bad []string
|
||||
keys := make([]string, 0, len(props))
|
||||
for k := range props {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, k := range keys {
|
||||
if !mp[k] {
|
||||
continue
|
||||
}
|
||||
v := props[k]
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
s, ok := v.(string)
|
||||
if !ok {
|
||||
bad = append(bad, fmt.Sprintf("%s=%v(非字符串)", k, v))
|
||||
continue
|
||||
}
|
||||
if s == "" {
|
||||
continue
|
||||
}
|
||||
if !isAbsURI(s) {
|
||||
bad = append(bad, fmt.Sprintf("%s=%q(非绝对 URI)", k, s))
|
||||
}
|
||||
}
|
||||
if len(bad) > 0 {
|
||||
return fmt.Errorf("uri_violation: %s", strings.Join(bad, "; "))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user