js-schema的Javascript例子1

亚马逊SPAPI

# 针对Javascript的验证器实施示例

对于JavaScript应用程序,Hyperjump - JSON Schema Validator (opens new window)库支持JSON Schema Draft 2019-09和自定义词汇表.下面的例子使用Node.js来演示如何利用[Hyperjump - JSON Schema Validator](https //github.com/hyperjump-io/json-schema-validator)库来验证具有Amazon产品类型定义Meta-Schema.实例的有效载荷,但是下面的例子代码在网络浏览器中无法工作该库不依赖Node.js,可以在网络浏览器中使用. 没有要求使用这个特定的库或示例实现. 亚马逊不为第三方JSON Schema库提供技术支持,这仅作为一个例子提供.

# Schema Configuration

当使用Hyperjump - JSON Schema Validator (opens new window)来验证带有自定义词汇的Amazon Product Type Definition Meta-Schema实例时,meta-schema被配置为一个JsonSchema实例,在Hyperjump - JSON Schema Core (opens new window).中注册了自定义词汇关键词验证

常量:

//亚马逊产品类型定义Meta-Schema.的$id
const schemaId = "https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1";
// 亚马逊产品类型定义的$id Luggage-Schema.
const luggageSchemaId = "https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/LUGGAGE";

// 亚马逊产品类型定义的本地副本 Meta-Schema.
const metaSchemaPath = "./amazon-product-type-definition-meta-schema-v1.json";

// 亚马逊产品类型定义Meta-Schema.实例的本地拷贝
const luggageSchemaPath = "./luggage.json";
1
2
3
4
5
6
7
8
9
10

配置Meta-Schema

// 添加自定义词汇和关键词
const { Core, Schema } = require("@hyperjump/json-schema-core" );
const maxUniqueItemsKeyword = require("./keywords/MaxUniqueItemsKeyword.js")
const maxUtf8ByteLengthKeyword = require("./keywords/MaxUtf8ByteLengthKeyword.js");
constminUtf8ByteLengthKeyword = require("./keywords/MinUtf8ByteLengthKeyword.js")
const customVocabularyId = "https://schemas.amazon.com/selling-partners/definitions/product-types/vocabulary/v1";

//配置模式JSON中定义的$vocabulary为vocabularyToken
Schema.setConfig(schemaId, "vocabularyToken", "$vocabulary" );

// 添加自定义词汇和关键词
Core.defineVocabulary(customVocabularyId, {
    maxUniqueItems : maxUniqueItemsKeyword,
    minUtf8ByteLength : minUtf8ByteLength关键字
    maxUtf8ByteLength : maxUtf8ByteLengthKeyword
});

// 在JsonSchema.add()中添加本地模式JSON,以使用模式的本地副本,而不是从网络中检索它们.
const JsonSchema = require("@hyperjump/json-schema" );

// 添加元模式
const metaSchemaJSON = JSON.parse(fs.readFileSync(metaSchemaPath), "utf8" );
JsonSchema.add(metaSchemaJSON, schemaId);

// 添加行李模式
const luggageSchemaJSON = JSON.parse(fs.readFileSync(luggageSchemaPath), "utf8" );
JsonSchema.add(luggageSchemaJSON, luggageSchemaId, schemaId);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

加载Meta-Schema实例:

//从JsonSchema实例中获取之前添加的行李模式
var luggageSchema = await JsonSchema.get(luggageSchemaId);
1
2

# Payload验证

有了Amazon产品类型定义Meta-Schema的实例作为JsonSchema实例加载,就可以使用instance.验证有效载荷

//为有效载荷加载JSON(这可以在代码中构建,从文件中读取,等等.).
const payload = JSON.parse(fs.readFileSync("./payload.json"), "utf8" );

//验证有效载荷并获得验证结果.
const result = await JsonSchema.validate(luggageSchema, payload, JsonSchema.BASIC);
const isValid = result.valid;
1
2
3
4
5
6

如果payload是有效的,isValid将返回true,并有一个空的错误关键字列表. 否则isValid将返回false,并有一个错误关键字列表.

示例验证信息:


{
    keyword: 'https://schemas.amazon.com/selling-partners/definitions/product-types/meta-schema/v1#minUtf8ByteLength',
    absoluteKeywordLocation: 'https://schemas.amazon.com/selling-partners/definitions/product-types/schema/v1/LUGGAGE#/properties/contribution_sku/items/properties/value/minUtf8ByteLength',
    instanceLocation: '#/contribution_sku/0/value',
    有效: false
}
1
2
3
4
5
6
7

# 关键词验证

Hyperjump - JSON Schema Core (opens new window) 提供了一个框架,可用于验证自定义词汇、关键词和其他工具.

下面的例子说明了JsonSchema的实现,它验证了Amazon产品类型定义Meta-Schema.实例中的自定义词汇

# MaxUniqueItemsKeyword script

const { Schema, Instance} = require("@hyperjump/json-schema-core" );

const compile = (schema, ast, parentSchema) =>
{
    const maxUniqueItems = Schema.value(schema);
    // 检索meta-schema中定义的选择器
    const selectors = parentSchema.value.selectors;
    返回 [maxUniqueItems, selectors];
}

const interpret = ([maxUniqueItems, selectors], instance, ast) => {
    如果(!Instance.typeOf(instance, "array" )) {
        返回false
    }

    let uniqueItems = new Array();
    // 对于示例模式中的每个实例,检索选择器组合
    instance.value.forEach(inst => {
        let selectorCombination = {};
        Object.entries(inst).forEach(([key,value]) => {
            if(selectors != undefined && selectors.includes(key)) {
                selectorCombination[key] = value;
            }});
        uniqueItems.push(JSON.stringify(selectorCombination));
    });

    let countMap = new Map();
    // 在countMap中添加每个唯一选择器组合的数量
    uniqueItems.forEach(item => {
        countMap.get(item) != undefined ? countMap.set(item, countMap.get(item)+ 1): countMap.set(item, 1);
    });
    // 过滤 countMap 中大于 maxUniqueItems 的值
    let filteredItems = Array.from(countMap.entries()).filter(item => {
        return item[1] > maxUniqueItems;
    });
    // 如果发现过滤的项目,则验证失败,否则成功
    return filteredItems.length > 0 ? false : true;
};

module.exports = { compile, interpret };
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

# MaxUtf8ByteLengthKeyword script

const { Schema, Instance} = require("@hyperjump/json-schema-core" );

const compile = (schema, ast) => Schema.value(schema);

const interpret = (maxUtf8ByteLength, instance, ast) => Instance.typeOf(instance, "string")&&
    Buffer.byteLength(Instance.value(instance)) <= maxUtf8ByteLength;

module.exports = { compile, interpret };
1
2
3
4
5
6
7
8

# MinUtf8ByteLengthKeyword script

const { Schema, Instance} = require("@hyperjump/json-schema-core" );

const compile = (schema, ast) => Schema.value(schema);

const interpret = (minUtf8ByteLength, instance, ast) =>
    Instance.typeOf(instance, "string") &amp;&amp; Buffer.byteLength(Instance.value(instance)) >= minUtf8ByteLength

module.exports = { compile, interpret };
1
2
3
4
5
6
7
8