...

Source file src/github.com/qri-io/jsonschema/keywords_conditional.go

Documentation: github.com/qri-io/jsonschema

     1  package jsonschema
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  
     7  	jptr "github.com/qri-io/jsonpointer"
     8  )
     9  
    10  // If defines the if JSON Schema keyword
    11  type If Schema
    12  
    13  // NewIf allocates a new If keyword
    14  func NewIf() Keyword {
    15  	return &If{}
    16  }
    17  
    18  // Register implements the Keyword interface for If
    19  func (f *If) Register(uri string, registry *SchemaRegistry) {
    20  	(*Schema)(f).Register(uri, registry)
    21  }
    22  
    23  // Resolve implements the Keyword interface for If
    24  func (f *If) Resolve(pointer jptr.Pointer, uri string) *Schema {
    25  	return nil
    26  }
    27  
    28  // ValidateKeyword implements the Keyword interface for If
    29  func (f *If) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    30  	schemaDebug("[If] Validating")
    31  	thenKW := currentState.Local.keywords["then"]
    32  	elseKW := currentState.Local.keywords["else"]
    33  
    34  	if thenKW == nil && elseKW == nil {
    35  		// no then or else for if, aborting validation
    36  		schemaDebug("[If] Aborting validation as no then or else is present")
    37  		return
    38  	}
    39  
    40  	subState := currentState.NewSubState()
    41  	subState.ClearState()
    42  	subState.DescendBase("if")
    43  	subState.DescendRelative("if")
    44  
    45  	subState.Errs = &[]KeyError{}
    46  	sch := Schema(*f)
    47  	sch.ValidateKeyword(ctx, subState, data)
    48  
    49  	currentState.Misc["ifResult"] = subState.IsValid()
    50  }
    51  
    52  // JSONProp implements the JSONPather for If
    53  func (f If) JSONProp(name string) interface{} {
    54  	return Schema(f).JSONProp(name)
    55  }
    56  
    57  // JSONChildren implements the JSONContainer interface for If
    58  func (f If) JSONChildren() (res map[string]JSONPather) {
    59  	return Schema(f).JSONChildren()
    60  }
    61  
    62  // UnmarshalJSON implements the json.Unmarshaler interface for If
    63  func (f *If) UnmarshalJSON(data []byte) error {
    64  	var sch Schema
    65  	if err := json.Unmarshal(data, &sch); err != nil {
    66  		return err
    67  	}
    68  	*f = If(sch)
    69  	return nil
    70  }
    71  
    72  // MarshalJSON implements the json.Marshaler interface for If
    73  func (f If) MarshalJSON() ([]byte, error) {
    74  	return json.Marshal(Schema(f))
    75  }
    76  
    77  // Then defines the then JSON Schema keyword
    78  type Then Schema
    79  
    80  // NewThen allocates a new Then keyword
    81  func NewThen() Keyword {
    82  	return &Then{}
    83  }
    84  
    85  // Register implements the Keyword interface for Then
    86  func (t *Then) Register(uri string, registry *SchemaRegistry) {
    87  	(*Schema)(t).Register(uri, registry)
    88  }
    89  
    90  // Resolve implements the Keyword interface for Then
    91  func (t *Then) Resolve(pointer jptr.Pointer, uri string) *Schema {
    92  	return (*Schema)(t).Resolve(pointer, uri)
    93  }
    94  
    95  // ValidateKeyword implements the Keyword interface for Then
    96  func (t *Then) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    97  	schemaDebug("[Then] Validating")
    98  	ifResult, okIf := currentState.Misc["ifResult"]
    99  	if !okIf {
   100  		schemaDebug("[Then] If result not found, skipping")
   101  		// if not found
   102  		return
   103  	}
   104  	if !(ifResult.(bool)) {
   105  		schemaDebug("[Then] If result is false, skipping")
   106  		// if was false
   107  		return
   108  	}
   109  
   110  	subState := currentState.NewSubState()
   111  	subState.DescendBase("then")
   112  	subState.DescendRelative("then")
   113  
   114  	sch := Schema(*t)
   115  	sch.ValidateKeyword(ctx, subState, data)
   116  	currentState.UpdateEvaluatedPropsAndItems(subState)
   117  }
   118  
   119  // JSONProp implements the JSONPather for Then
   120  func (t Then) JSONProp(name string) interface{} {
   121  	return Schema(t).JSONProp(name)
   122  }
   123  
   124  // JSONChildren implements the JSONContainer interface for Then
   125  func (t Then) JSONChildren() (res map[string]JSONPather) {
   126  	return Schema(t).JSONChildren()
   127  }
   128  
   129  // UnmarshalJSON implements the json.Unmarshaler interface for Then
   130  func (t *Then) UnmarshalJSON(data []byte) error {
   131  	var sch Schema
   132  	if err := json.Unmarshal(data, &sch); err != nil {
   133  		return err
   134  	}
   135  	*t = Then(sch)
   136  	return nil
   137  }
   138  
   139  // MarshalJSON implements the json.Marshaler interface for Then
   140  func (t Then) MarshalJSON() ([]byte, error) {
   141  	return json.Marshal(Schema(t))
   142  }
   143  
   144  // Else defines the else JSON Schema keyword
   145  type Else Schema
   146  
   147  // NewElse allocates a new Else keyword
   148  func NewElse() Keyword {
   149  	return &Else{}
   150  }
   151  
   152  // Register implements the Keyword interface for Else
   153  func (e *Else) Register(uri string, registry *SchemaRegistry) {
   154  	(*Schema)(e).Register(uri, registry)
   155  }
   156  
   157  // Resolve implements the Keyword interface for Else
   158  func (e *Else) Resolve(pointer jptr.Pointer, uri string) *Schema {
   159  	return (*Schema)(e).Resolve(pointer, uri)
   160  }
   161  
   162  // ValidateKeyword implements the Keyword interface for Else
   163  func (e *Else) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
   164  	schemaDebug("[Else] Validating")
   165  	ifResult, okIf := currentState.Misc["ifResult"]
   166  	if !okIf {
   167  		// if not found
   168  		return
   169  	}
   170  	if ifResult.(bool) {
   171  		// if was true
   172  		return
   173  	}
   174  
   175  	subState := currentState.NewSubState()
   176  	subState.DescendBase("else")
   177  	subState.DescendRelative("else")
   178  
   179  	sch := Schema(*e)
   180  	sch.ValidateKeyword(ctx, subState, data)
   181  }
   182  
   183  // JSONProp implements the JSONPather for Else
   184  func (e Else) JSONProp(name string) interface{} {
   185  	return Schema(e).JSONProp(name)
   186  }
   187  
   188  // JSONChildren implements the JSONContainer interface for Else
   189  func (e Else) JSONChildren() (res map[string]JSONPather) {
   190  	return Schema(e).JSONChildren()
   191  }
   192  
   193  // UnmarshalJSON implements the json.Unmarshaler interface for Else
   194  func (e *Else) UnmarshalJSON(data []byte) error {
   195  	var sch Schema
   196  	if err := json.Unmarshal(data, &sch); err != nil {
   197  		return err
   198  	}
   199  	*e = Else(sch)
   200  	return nil
   201  }
   202  
   203  // MarshalJSON implements the json.Marshaler interface for Else
   204  func (e Else) MarshalJSON() ([]byte, error) {
   205  	return json.Marshal(Schema(e))
   206  }
   207  

View as plain text