2017-03-20 2 views
2

Ich möchte ein Regexp-Muster für ein Feld basierend auf den Daten in einem anderen angeben. Ist das möglich? Ich habe Switch und $ Daten versucht, aber nicht sicher, wie man sie benutzt. zum Beispiel, wenn Daten wie folgt aussehen:ajv bedingte Schema-Validierung basierend auf Daten

{ 
    "contacts":[ 
     { 
     "mode":"Email", 
     "contact":"[email protected]" 
     }, 
     { 
     "mode":"Phone", 
     "contact":"111-555-1234" 
     } 
    ] 
} 

und Schema sieht ungefähr so ​​aus:

"$schema":"http://json-schema.org/draft-04/schema#", 
    "type":"object", 
    "properties":{ 
     "Contacts":{ 
     "type":"array", 
     "minItems":1, 
     "items":{ 
      "type":"object", 
      "properties":{ 
       "mode":{ 
        "type":"string", 
        "enum":[ 
        "Email", 
        "Phone" 
        ] 
       }, 
       "contact":{ 
        "type":"string", 
        "pattern":"?????" 
       } 
      }, 
      "required":[ 
       "mode", 
       "contact" 
      ] 
     } 
     } 
    } 
} 

Wie kann ich das Muster des Kontaktsatzes basierend auf Daten in Modus, so dass, wenn Modus E-Mail, Es validiert den Kontakt mit einem Regexp für ein E-Mail-Format, und wenn der Modus Telefon ist, validiert es den Kontakt mit einem Regexp für ein Telefonformat? Ich habe die Regexp für jeden. Ich brauche die Logik, um das eine oder das andere zu wählen.

Antwort

5

Es gibt mehrere Möglichkeiten, es zu tun

Bestimmungen haften (Profis: draft-04 kompatibel, Nachteile: Fehlerberichterstattung ein bisschen ausführlicher ist - Sie werden Fehler von beiden Teilschemen, wenn keine Spiele)

{ 
    "type": "object", 
    "properties": { 
     "Contacts": { 
     "type": "array", 
     "minItems": 1, 
     "items": { 
      "type": "object", 
      "anyOf": [ 
       { 
        "properties": { 
        "mode": {"enum": ["Email"]}, 
        "contact": { 
         "type": "string", 
         "format": "email" 
        } 
        } 
       }, 
       { 
        "properties": { 
        "mode": {"enum": ["Phone"]}, 
        "contact": { 
         "type": "string", 
         "pattern": "phone_pattern" 
        } 
        } 
       } 
      ], 
      "required": ["mode", "contact"] 
     } 
     } 
    } 
} 

if/then/else (available in ajv-keywords package, Profis: Fehlerberichterstattung macht mehr s Ense, accepted to be included in draft-07, Nachteile: zur Zeit nicht Standard):

{ 
    "type": "object", 
    "properties": { 
     "Contacts": { 
     "type": "array", 
     "minItems": 1, 
     "items": { 
      "type": "object", 
      "properties": { 
       "mode": {"type": "string", "enum": ["Email", "Phone"]}, 
       "contact": {"type": "string"} 
      }, 
      "if": { 
       "properties": { 
        "mode": {"enum": ["Email"]} 
       } 
      }, 
      "then": { 
       "properties": { 
        "contact": {"format": "email"} 
       } 
      }, 
      "else": { 
       "properties": { 
        "contact": {"pattern": "phone_pattern"} 
       } 
      } 
      "required": ["mode", "contact"] 
     } 
     } 
    } 
} 

wählen (available in ajv-keywords package, Profis: prägnanter als if/then/else, vor allem, wenn es mehr als zwei mögliche Werte, Nachteile: not on the standard track yet, aber man kann es unterstützen :) erfordert ermöglicht $ Datenreferenz und AJV v5.xx):

{ 
    "type": "object", 
    "properties": { 
     "Contacts": { 
     "type": "array", 
     "minItems": 1, 
     "items": { 
      "type": "object", 
      "properties": { 
       "mode": {"type": "string"}, 
       "contact": {"type": "string"} 
      }, 
      "select": { "$data": "0/mode" }, 
      "selectCases": { 
       "Email": { 
        "properties": { 
        "contact": {"format": "email"} 
        } 
       }, 
       "Phone": { 
        "properties": { 
        "contact": {"pattern": "phone_pattern"} 
        } 
       } 
      }, 
      "selectDefault": false, 
      "required": ["mode", "contact"] 
     } 
     } 
    } 
} 

Ich bevorzuge die letzte Option.

Verwandte Themen