You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为瑞典驾照类可验证凭证定义自定义@context?

关于瑞典驾照Verifiable Credential自定义上下文的问题解答

1. 如何定义匹配凭证结构的自定义上下文

直接编写JSON-LD格式的自定义上下文文件,映射你需要的瑞典驾照专属字段,示例如下:

// https://www.example.org/2022/swedish-drivers-license.jsonld
{
  "@context": {
    "@version": 1.1,
    "sd": "https://www.example.org/2022/swedish-drivers-license#",
    "ssn": { "@id": "sd:ssn", "@type": "xsd:string" },
    "driverLicenseNumber": { "@id": "sd:driverLicenseNumber", "@type": "xsd:string" },
    "vehicleClasses": { "@id": "sd:vehicleClasses", "@type": "xsd:string" },
    "issueDate": { "@id": "sd:issueDate", "@type": "xsd:date" },
    "expiryDate": { "@id": "sd:expiryDate", "@type": "xsd:date" },
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  }
}

然后在你的Verifiable Credential中,将这个自定义上下文加入@context数组即可生效:

{
  "@context": [
    "https://www.w3.org/2018/credentials/v1",
    "https://www.example.org/2022/swedish-drivers-license.jsonld"
  ],
  "type": ["VerifiableCredential", "SwedishDriversLicense"],
  "issuer": "did:example:swedish-transport-agency",
  "credentialSubject": {
    "id": "did:example:anna-svensson",
    "name": "Anna Svensson",
    "ssn": "19900101-1234",
    "driverLicenseNumber": "SE-ABC123",
    "vehicleClasses": ["B", "BE"]
  }
}

核心逻辑:用自定义命名空间sd:避免术语冲突,给每个专属字段分配唯一URI标识,并指定对应的数据类型。

2. 是否必须基于第三方本体,能否自行定义基础术语

完全不需要依赖第三方本体,你可以自由定义包括ssn在内的基础术语。
JSON-LD的设计初衷就是支持自定义语义,只要给术语分配一个唯一的URI(可以是你自己域名下的标识,不需要对应实际可访问的网页)即可。比如简化版的自定义上下文可以直接用@vocab批量绑定命名空间:

{
  "@context": {
    "@version": 1.1,
    "@vocab": "https://www.example.org/2022/swedish-drivers-license#",
    "ssn": "xsd:string",
    "driverLicenseNumber": "xsd:string",
    "xsd": "http://www.w3.org/2001/XMLSchema#"
  }
}

这里的ssn就是你完全自定义的术语,不需要关联任何第三方本体。

3. 如何将name字段定义为必填的string类型

需要分两步实现:

第一步:在上下文里声明name的类型为字符串

如果标准VC上下文没有明确name的类型,你可以在自定义上下文中覆盖定义:

"name": { "@id": "http://schema.org/name", "@type": "xsd:string" }

第二步:用JSON Schema强制必填规则

JSON-LD本身不处理字段的必填性,必须通过VC的credentialSchema字段关联JSON Schema来验证。示例Schema:

// https://www.example.org/2022/swedish-drivers-license-schema.json
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "type": "object",
  "properties": {
    "credentialSubject": {
      "type": "object",
      "properties": {
        "name": { "type": "string" },
        "ssn": { "type": "string" }
      },
      "required": ["name"] // 指定name为必填字段
    }
  },
  "required": ["@context", "type", "issuer", "credentialSubject"]
}

然后在VC中引用这个Schema:

{
  "@context": [...],
  "type": ["VerifiableCredential", "SwedishDriversLicense"],
  "issuer": "...",
  "credentialSchema": {
    "id": "https://www.example.org/2022/swedish-drivers-license-schema.json",
    "type": "JsonSchemaValidator2018"
  },
  "credentialSubject": {
    "id": "...",
    "name": "Anna Svensson", // 必须存在且为字符串
    // 其他字段...
  }
}

验证时会强制检查credentialSubject中是否存在name字段,且类型为字符串。

内容的提问来源于stack exchange,提问作者Frank Weslien

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 02:50:40