2011-11-18 16 views
32

Sto provando a convalidare la mia API JSON utilizzando node.js + json-schema.js da commonjs-utils. Solo una singola convalida era facile ma non riusciva a trovare il modo giusto come gestire più file di schema per abilitare il referenziamento reciproco.Come gestire più file di schemi JSON?

Supponiamo di avere due modelli & due API.

// book 
{ 
    "type": "object", 
    "properties": { 
     "title": { "type": "string" }, 
     "author": { "type": "string" } 
    } 
} 
// author 
{ 
    "type": "object", 
    "properties": { 
     "first_name": { "type": "string" }, 
     "last_name": { "type": "string" } 
    } 
} 
// authors API 
{ 
    "type": "array", 
    "items": { "$ref": "author" } 
} 
// books API: list of books written by same author 
{ 
    "type": "object", 
    "properties": { 
    "author": { "$ref": "author" } 
    "books": { "type": "array", "items": { "$ref": "book" } } 
    } 
} 

Ogni schema deve essere diviso in un file separato ed essere online? Oppure posso unire in un file schema singolo come di seguito? Se è possibile, come posso fare riferimento allo schema locale?

// single schema file { 
    "book": { ... }, 
    "author": { ... }, 
    "authors": { ... }, 
    "books": { ... } } 

risposta

46

in JSON schemi, è possibile inserire uno schema per file e quindi accedervi utilizzando il proprio URL (dove è memorizzato), o una grande schema con id tags.

qui è per un unico grande file:

{ 
    "id": "#root", 
    "properties": { 
     "author": { 
      "id": "#author", 
      "properties": { 
       "first_name": { 
        "type": "string" 
       }, 
       "last_name": { 
        "type": "string" 
       } 
      }, 
      "type": "object" 
     }, 
     // author 
     "author_api": { 
      "id": "#author_api", 
      "items": { 
       "$ref": "author" 
      }, 
      "type": "array" 
     }, 
     // authors API 
     "book": { 
      "id": "#book", 
      "properties": { 
       "author": { 
        "type": "string" 
       }, 
       "title": { 
        "type": "string" 
       } 
      }, 
      "type": "object" 
     }, 
     // books API: list of books written by same author 
     "books_api": { 
      "id": "#books_api", 
      "properties": { 
       "author": { 
        "$ref": "author" 
       }, 
       "books": { 
        "items": { 
         "$ref": "book" 
        }, 
        "type": "array" 
       } 
      }, 
      "type": "object" 
     } 
    } 
} 

Si può quindi fare riferimento il vostro validatore a una di quelle schemi secondari (che vengono definiti con una id).

Dall'esterno dello schema, questo:

{ "$ref": "url://to/your/schema#root/properties/book" } 

è equivalente a questo:

{ "$ref": "url://to/your/schema#book" } 

& hellip; che è equivalente, da dentro, a questo:

{ "$ref": "#root/properties/book" } 

o del presente (ancora dall'interno):

{ "$ref": "#book" } 

Vedi il mio answer here per ulteriori informazioni.

+0

C'è un modo per creare oggetti con file di schema separati in node.js? – uzay95

+1

Gli schemi @ uzay95 non sono classi. Uno schema è usato per definire e validare la struttura di un oggetto, una classe definisce la struttura di un oggetto dopo la sua inizializzazione. Se hai bisogno di lezioni in js, prova a guardare Typescript, AtScript o ES6. O semplicemente inserisci un modulo o un metodo factory di costruzione all'interno di un file esterno che includerai in node.js. –

Problemi correlati