...

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

Documentation: github.com/qri-io/jsonschema

     1  package jsonschema
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  
     7  	jptr "github.com/qri-io/jsonpointer"
     8  )
     9  
    10  // MultipleOf defines the multipleOf JSON Schema keyword
    11  type MultipleOf float64
    12  
    13  // NewMultipleOf allocates a new MultipleOf keyword
    14  func NewMultipleOf() Keyword {
    15  	return new(MultipleOf)
    16  }
    17  
    18  // Register implements the Keyword interface for MultipleOf
    19  func (m *MultipleOf) Register(uri string, registry *SchemaRegistry) {}
    20  
    21  // Resolve implements the Keyword interface for MultipleOf
    22  func (m *MultipleOf) Resolve(pointer jptr.Pointer, uri string) *Schema {
    23  	return nil
    24  }
    25  
    26  // ValidateKeyword implements the Keyword interface for MultipleOf
    27  func (m MultipleOf) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    28  	schemaDebug("[MultipleOf] Validating")
    29  	if num, ok := data.(float64); ok {
    30  		div := num / float64(m)
    31  		if float64(int(div)) != div {
    32  			currentState.AddError(data, fmt.Sprintf("must be a multiple of %f", m))
    33  		}
    34  	}
    35  }
    36  
    37  // Maximum defines the maximum JSON Schema keyword
    38  type Maximum float64
    39  
    40  // NewMaximum allocates a new Maximum keyword
    41  func NewMaximum() Keyword {
    42  	return new(Maximum)
    43  }
    44  
    45  // Register implements the Keyword interface for Maximum
    46  func (m *Maximum) Register(uri string, registry *SchemaRegistry) {}
    47  
    48  // Resolve implements the Keyword interface for Maximum
    49  func (m *Maximum) Resolve(pointer jptr.Pointer, uri string) *Schema {
    50  	return nil
    51  }
    52  
    53  // ValidateKeyword implements the Keyword interface for Maximum
    54  func (m Maximum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    55  	schemaDebug("[Maximum] Validating")
    56  	if num, ok := data.(float64); ok {
    57  		if num > float64(m) {
    58  			currentState.AddError(data, fmt.Sprintf("must be less than or equal to %f", m))
    59  		}
    60  	}
    61  }
    62  
    63  // ExclusiveMaximum defines the exclusiveMaximum JSON Schema keyword
    64  type ExclusiveMaximum float64
    65  
    66  // NewExclusiveMaximum allocates a new ExclusiveMaximum keyword
    67  func NewExclusiveMaximum() Keyword {
    68  	return new(ExclusiveMaximum)
    69  }
    70  
    71  // Register implements the Keyword interface for ExclusiveMaximum
    72  func (m *ExclusiveMaximum) Register(uri string, registry *SchemaRegistry) {}
    73  
    74  // Resolve implements the Keyword interface for ExclusiveMaximum
    75  func (m *ExclusiveMaximum) Resolve(pointer jptr.Pointer, uri string) *Schema {
    76  	return nil
    77  }
    78  
    79  // ValidateKeyword implements the Keyword interface for ExclusiveMaximum
    80  func (m ExclusiveMaximum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
    81  	schemaDebug("[ExclusiveMaximum] Validating")
    82  	if num, ok := data.(float64); ok {
    83  		if num >= float64(m) {
    84  			currentState.AddError(data, fmt.Sprintf("%f must be less than %f", num, m))
    85  		}
    86  	}
    87  }
    88  
    89  // Minimum defines the minimum JSON Schema keyword
    90  type Minimum float64
    91  
    92  // NewMinimum allocates a new Minimum keyword
    93  func NewMinimum() Keyword {
    94  	return new(Minimum)
    95  }
    96  
    97  // Register implements the Keyword interface for Minimum
    98  func (m *Minimum) Register(uri string, registry *SchemaRegistry) {}
    99  
   100  // Resolve implements the Keyword interface for Minimum
   101  func (m *Minimum) Resolve(pointer jptr.Pointer, uri string) *Schema {
   102  	return nil
   103  }
   104  
   105  // ValidateKeyword implements the Keyword interface for Minimum
   106  func (m Minimum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
   107  	schemaDebug("[Minimum] Validating")
   108  	if num, ok := data.(float64); ok {
   109  		if num < float64(m) {
   110  			currentState.AddError(data, fmt.Sprintf("must be less than or equal to %f", m))
   111  		}
   112  	}
   113  }
   114  
   115  // ExclusiveMinimum defines the exclusiveMinimum JSON Schema keyword
   116  type ExclusiveMinimum float64
   117  
   118  // NewExclusiveMinimum allocates a new ExclusiveMinimum keyword
   119  func NewExclusiveMinimum() Keyword {
   120  	return new(ExclusiveMinimum)
   121  }
   122  
   123  // Register implements the Keyword interface for ExclusiveMinimum
   124  func (m *ExclusiveMinimum) Register(uri string, registry *SchemaRegistry) {}
   125  
   126  // Resolve implements the Keyword interface for ExclusiveMinimum
   127  func (m *ExclusiveMinimum) Resolve(pointer jptr.Pointer, uri string) *Schema {
   128  	return nil
   129  }
   130  
   131  // ValidateKeyword implements the Keyword interface for ExclusiveMinimum
   132  func (m ExclusiveMinimum) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
   133  	schemaDebug("[ExclusiveMinimum] Validating")
   134  	if num, ok := data.(float64); ok {
   135  		if num <= float64(m) {
   136  			currentState.AddError(data, fmt.Sprintf("%f must be less than %f", num, m))
   137  		}
   138  	}
   139  }
   140  

View as plain text