@ adobe/ 结构化数据验证器
一个JavaScript库,用于根据 Schema.org 的规格和 Google Rich Results 的要求验证和解析结构化数据. 这个库确保了您的结构化数据既符合Schema.org标准,也符合Google对丰富结果的具体要求,帮助优化了您搜索引擎和其他平台的内容.
特征
- 对照 Schema.org 和 Google Rich Results 的规格验证结构化数据
- 确保遵守谷歌的结构化数据准则
- 具有自定义类型处理器的广泛验证系统
安装
npm install @adobe/structured-data-validator
使用量
这个图书馆与 @marbec/web-自动提取器 以验证从网页中提取的结构化数据.
import { Validator } from '@adobe/structured-data-validator';
import WebAutoExtractor from '@marbec/web-auto-extractor';
// First, extract structured data from HTML
const extractor = new WebAutoExtractor({ addLocation: true, embedSource: ['rdfa', 'microdata'] });
const extractedData = extractor.parse(sampleHTML);
// Fetch the current schema.org schema
const schemaOrgJson = await (await fetch('https://schema.org/version/latest/schemaorg-all-https.jsonld')).json();
// Create a validator instance
const validator = new Validator(schemaOrgJson);
// Validate the extracted structured data
const results = await validator.validate(extractedData);
}
校验器期望输出格式来自 @marbec/web-auto-extractor,包括:
- JSON-LD结构数据
- 微数据
- 卢旺达国防军a
验证结果
验证器返回一系列验证问题 。 每个问题对象包含:
issueMessage- 人类可读错误或警告信息severity- 随便'ERROR'或者说'WARNING'path- 代表问题发生地数据结构中的位置的矩阵fieldNames- 导致验证问题的战地路径阵列(例如,['price'], (中文(简体) ),['offers.price'],或['aggregateRating', 'offers', 'review'](复合条件)location- 原始来源中的字符位置(如果有的话)
示例验证结果 :
[
{
issueMessage: 'Required attribute "price" is missing',
severity: 'ERROR',
path: [{ type: 'Product', index: 0 }],
fieldNames: ['price'],
location: '35,100',
},
];
浏览器
您可以使用以下命令在浏览器中直接运行解析器和验证器:
const { default: WebAutoExtractor } = await import(
'https://unpkg.com/@marbec/web-auto-extractor@latest/dist/index.js'
);
const { default: Validator } = await import(
'https://unpkg.com/@adobe/structured-data-validator@latest/src/index.js'
);
const extractedData = new WebAutoExtractor({
addLocation: true,
embedSource: ['rdfa', 'microdata'],
}).parse(document.documentElement.outerHTML);
console.log(extractedData);
const schemaOrgJson = await (
await fetch('https://schema.org/version/latest/schemaorg-all-https.jsonld')
).json();
await new Validator(schemaOrgJson).validate(extractedData);
发展
先决条件
- 节点.js(推荐最新LTS版本)
- 无
设置
- 清除仓库
- 安装依赖性 :
npm install
可用的脚本
npm test- 进行覆盖测试npm run lint- 运行ESLintnpm run format- 检查代码格式npm run format:fix- 解决代码格式问题
除错日志
要启用调试日志并查看详细的验证输出, 请设置 debug 属性改为 true 在你身边 Validator 实例:
const validator = new Validator();
validator.debug = true; // Enable debug logging
这将在验证过程中将额外信息打印到控制台,这对开发和排除出故障很有用.
依赖关系
- @marbec/web-自动提取器 - 从网页提取结构化数据