2013-05-10 18 views
16

Provare a scrivere uno schema JSON che utilizza RegEx per convalidare un valore di un elemento.Utilizzo di RegEx in JSON Schema

Avere un elemento denominato progBinaryName il cui valore deve adhrere a questa stringa RegEx "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$".

Impossibile trovare esercitazioni o esempi che spieghino in realtà l'utilizzo di RegEx in uno schema JSON.

Qualsiasi aiuto/informazione sarebbe apprezzato GRAZIE!

Grazie, D

JSON SCHEMA

{ 
    "name": "string", 
    "properties": { 
     "progName": { 
      "type": "string", 
      "description": "Program Name", 
      "required": true 
     }, 
     "ID": { 
      "type": "string", 
      "description": "Identifier", 
      "required": true 
     }, 
     "progVer": { 
      "type": "string", 
      "description": "Version number", 
      "required": true 
     }, 
     "progBinaryName": { 
      "type": "string", 
      "description": "Actual name of binary", 
      "patternProperties": { 
       "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$" 
      }, 
      "required": true 
     } 
    } 
} 

ERRORI:

Attenzione! Meglio controllare il tuo JSON.

grado non è un tipo di richiesta - http://json-schema.org/draft-03/hyper-schema#


schema è valido JSON, ma non uno schema valido. risultati


di convalida: insufficienza

[ { 
    "level" : "warning", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "" 
    }, 
    "domain" : "syntax", 
    "message" : "unknown keyword(s) found; ignored", 
    "ignored" : [ "name" ] 
}, { 
    "level" : "error", 
    "domain" : "syntax", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "/properties/ID" 
    }, 
    "keyword" : "required", 
    "message" : "value has incorrect type", 
    "expected" : [ "array" ], 
    "found" : "boolean" 
}, { 
    "level" : "error", 
    "domain" : "syntax", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "/properties/progBinaryName" 
    }, 
    "keyword" : "required", 
    "message" : "value has incorrect type", 
    "expected" : [ "array" ], 
    "found" : "boolean" 
}, { 
    "level" : "error", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "/properties/progBinaryName/patternProperties/progBinaryName" 
    }, 
    "domain" : "syntax", 
    "message" : "JSON value is not a JSON Schema: not an object", 
    "found" : "string" 
}, { 
    "level" : "error", 
    "domain" : "syntax", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "/properties/progName" 
    }, 
    "keyword" : "required", 
    "message" : "value has incorrect type", 
    "expected" : [ "array" ], 
    "found" : "boolean" 
}, { 
    "level" : "error", 
    "domain" : "syntax", 
    "schema" : { 
     "loadingURI" : "#", 
     "pointer" : "/properties/progVer" 
    }, 
    "keyword" : "required", 
    "message" : "value has incorrect type", 
    "expected" : [ "array" ], 
    "found" : "boolean" 
} ] 

Problem with schema#/properties/progBinaryName/patternProperties/progBinaryName : Instance is not a required type 
Reported by http://json-schema.org/draft-03/hyper-schema# 
Attribute "type" (["object"]) 
+1

Che cosa non funziona? (Potresti voler mettere il trattino alla fine della classe del personaggio) –

+0

Qualsiasi convalida online non funziona correttamente. – Destroyer

+2

Che cosa significa "non funziona correttamente"? Hai dei falsi positivi? Ottieni falsi negativi? Hai qualche tipo di errore? –

risposta

35

Per testare un valore stringa (non un nome di proprietà) contro una RegEx, è necessario utilizzare la parola chiave "pattern":

{ 
    "type": "object", 
    "properties": { 
     "progBinaryName": { 
      "type": "string", 
      "pattern": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$" 
     } 
    } 
} 

P.S. - se si desidera che il motivo corrisponda alla chiave per la proprietà (non il valore), è necessario utilizzare "patternProperties" (è come "properties", ma la chiave è una RegEx).

10

la sintassi dello schema JSON non è corretto. Cambiare

"patternProperties": { 
    "progBinaryName": "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$" 
    } 

a

"patternProperties": { 
    "^[A-Za-z0-9 -_]+_Prog\\.(exe|EXE)$": {} 
    } 
+1

Grazie per aver fornito una risposta e non aver perso tempo. Molto, molto apprezzato! – Destroyer

+0

Non si trattava di testare il * valore *, non il nome della proprietà? – cloudfeet

+0

Hai ragione. La tua risposta dovrebbe essere accettata. –