目的
TypeScriptで作成したAPIで、受け取ったリクエストに対してバリデーションをかけたい。
質問点
エラーの解決方法、または、別の実装方法があれば教えていただきたいです。
実行したこと
使用したもの
- QuickType (typescriptのコードからJSON Schemaを生成する)
- ajv (json schema を用いたバリデーションライブラリー)
コード
typescript
1interface Content { 2 url: string; 3} 4 5interface Message { 6 id?: number; 7 title: string; 8 content?: Content; 9}
上記のようなinterfaceからQuickTypeを用いて下記のようなJSON schemaを作成。
json
1{ 2 "$schema": "http://json-schema.org/draft-07/schema#", 3 "definitions": { 4 "Content": { 5 "title": "Content", 6 "type": "object", 7 "properties": { 8 "url": { 9 "type": "string", 10 "title": "url" 11 } 12 }, 13 "required": [ 14 "url" 15 ] 16 }, 17 "Message": { 18 "title": "Message", 19 "type": "object", 20 "properties": { 21 "id": { 22 "type": "number", 23 "title": "id" 24 }, 25 "title": { 26 "type": "string", 27 "title": "title" 28 }, 29 "content": { 30 "$ref": "#/definitions/Content", 31 "title": "content" 32 } 33 }, 34 "required": [ 35 "title" 36 ] 37 } 38 } 39}
そしてajvでvalidateをかけようとすると、下記のようなエラーが発生します。
{ [Error: can't resolve reference #/definitions/Content from id #] message: 'can\'t resolve reference #/definitions/Content from id #', missingRef: '#/definitions/Content', missingSchema: '' }
あなたの回答
tips
プレビュー