第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何使 json $ref本地文件?

如何使 json $ref本地文件?

手掌心 2022-09-29 17:31:24
我正在我的節(jié)點(diǎn).js項(xiàng)目中使用 AJV 包。我正在嘗試根據(jù)幾個(gè)架構(gòu)文件驗(yàn)證一些數(shù)據(jù)。這兩個(gè)架構(gòu)文件位于同一目錄中:/dir    |    parent_schema.json    |    sub_schema.json/data    |    data.json我試圖得到一個(gè)超級(jí)簡(jiǎn)單的例子,但我遇到了麻煩。 看來(lái):$refparent_schema.json{  "properties": {    "foo": { "type": "string" },    "bar": { "$ref": "sub_schema.json" }  }}看起來(lái)像這樣:sub_schema.json{  "properties": {    "sub1": { "type": "string" },  }}為了完整起見(jiàn),我試圖驗(yàn)證我的:如下所示:data.json{  "foo": "whatever",  "bar": {    "sub1": "sometext"  }}我遇到的問(wèn)題是我的道路。我從 AJV 收到此錯(cuò)誤:$refMissingRefError {    message: "can't resolve reference subschema1.json from id #"    missingRef: "subschema1.json"    missingSchema: "subschema1.json"}有人看到我的道路出了什么問(wèn)題嗎?我知道您還應(yīng)該使用 來(lái)選擇要匹配的特定屬性,但我希望使用整個(gè)架構(gòu)。#
查看完整描述

1 回答

?
縹緲止盈

TA貢獻(xiàn)2041條經(jīng)驗(yàn) 獲得超4個(gè)贊

這是一個(gè)常見(jiàn)的誤解,以某種方式“加載”文件。$ref

看看 ajv.js.org 是怎么說(shuō)的:

$ref使用架構(gòu)$id作為基本 URI 作為 URI 引用進(jìn)行解析(請(qǐng)參閱示例)。

和:

您不必在用作架構(gòu)$id的 URI 上托管架構(gòu)文件。這些 URI 僅用于標(biāo)識(shí)架構(gòu),根據(jù) JSON 架構(gòu)規(guī)范,驗(yàn)證程序不應(yīng)期望能夠從這些 URI 下載架構(gòu)。

Ajv 不會(huì)嘗試從以下位置加載此架構(gòu):例如:stack://over.flow/string

{

  "$id": "stack://over.flow/string",

  "type": "string"

}

如果要在另一個(gè)架構(gòu)中引用該架構(gòu),它們都需要具有相同的基本URI,例如,stack://over.flow/


{

  "$id":  "stack://over.flow/object",

  "type": "object",

  "properties": {

    "a": { "$ref": "string#" }

  }

}

這里說(shuō)“在 stack://over.flow/string 導(dǎo)入架構(gòu)”,所以你最終得到:{ "$ref": "string#" }


{

  "$id":  "stack://over.flow/object",

  "type": "object",

  "properties": {

    "a": {

      "$id": "stack://over.flow/string",

      "type": "string"

    }

  }

}

這允許您組合小架構(gòu):


const ajv = new Ajv;


ajv.addSchema({

  "$id": "stack://over.flow/string",

  "type": "string"

});


ajv.addSchema({

  "$id": "stack://over.flow/number",

  "type": "number"

});


const is_string = ajv.getSchema("stack://over.flow/string");

const is_number = ajv.getSchema("stack://over.flow/number");


console.log(is_string('aaa'), is_string(42));

console.log(is_number('aaa'), is_number(42));


const is_ab = ajv.compile({

  "$id":  "stack://over.flow/object",

  "type": "object",

  "properties": {

    "a": { "$ref": "string#" },

    "b": { "$ref": "number#" }

  }

});


console.log(is_ab({a: "aaa", b: 42}));

console.log(is_ab({a: 42, b: "aaa"}));

<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>

(請(qǐng)注意,在您的示例中,兩個(gè)架構(gòu)都不正確。兩者都缺少 {“type”: “對(duì)象”}。


要回答您的問(wèn)題:


const ajv = new Ajv;


ajv.addSchema({

  "$id": "stack://over.flow/parent.schema",

  "type": "object",

  "properties": {

    "foo": { "type": "string" },

    "bar": { "$ref": "child.schema#" }

  }

});


ajv.addSchema({

  "$id": "stack://over.flow/child.schema",

  "type": "object",

  "properties": {

    "sub1": { "type": "string" },

  }

});


const is_parent = ajv.getSchema("stack://over.flow/parent.schema");

const is_child = ajv.getSchema("stack://over.flow/child.schema");


console.log(is_parent({

  "foo": "whatever",

  "bar": {

    "sub1": "sometext"

  }

}));

<script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/6.12.2/ajv.min.js"></script>


查看完整回答
反對(duì) 回復(fù) 2022-09-29
  • 1 回答
  • 0 關(guān)注
  • 127 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)