元素模板验证器
根据 杰森·斯克马。 。 。 .
安装
npm i --save @bpmn-io/element-templates-validator
使用量
考虑到 示例元素模板数字 :
import { validate } from '@bpmn-io/element-templates-validator';
import sample from './test/fixtures/rpa-broken.json';
const {
valid,
errors,
warnings
} = validate(sample);
if (!valid) {
console.error('Invalid JSON detected:', errors);
}
这将打印样本内错误和警告的详细信息:
[
{
"keyword": "type",
"dataPath": "",
"schemaPath": "#/type",
"params": {
"type": "object"
},
"message": "must be object",
"dataPointer": {
"value": {
"line": 0,
"column": 0,
"pos": 0
},
"valueEnd": {
"line": 177,
"column": 1,
"pos": 4825
}
}
}
...
]
您也可以通过字符串模板,以确保 dataPointers 中,错误反映了原字符串中的正确位置.
import { validate } from '@bpmn-io/element-templates-validator';
import { readFileSync } from 'node:fs';
const sample = readFileSync('./test/fixtures/rpa-broken.json', 'utf-8');
const {
valid,
errors,
warnings
} = validate(sample);
if (!valid) {
console.error('Invalid JSON detected:', errors);
}
如果一个解析过的JSON对象通过,则 dataPointers 将假设源模板格式化为 JSON.stringify(template, null, 2)。 。 。 .
也可以同时验证多个对象. 在这种情况下,模板列表只支持对象,而不是字符串.
import { validateAll } from '@bpmn-io/element-templates-validator';
import samples from './test/fixtures/multiple-errors.json';
const {
valid,
results
} = validateAll(samples);
if (!valid) {
console.error('Invalid JSON objects detected:', results.filter(r => !r.valid));
}
许可证
麻省理工学院