github.com/kaptinlin/jsonschema@v0.4.6/keywords.go (about)

     1  package jsonschema
     2  
     3  // Keyword represents a schema keyword that can be applied to any schema
     4  type Keyword func(*Schema)
     5  
     6  // ===============================
     7  // String keywords
     8  // ===============================
     9  
    10  // MinLen sets the minLength keyword
    11  func MinLen(min int) Keyword {
    12  	return func(s *Schema) {
    13  		f := float64(min)
    14  		s.MinLength = &f
    15  	}
    16  }
    17  
    18  // MaxLen sets the maxLength keyword
    19  func MaxLen(max int) Keyword {
    20  	return func(s *Schema) {
    21  		f := float64(max)
    22  		s.MaxLength = &f
    23  	}
    24  }
    25  
    26  // Pattern sets the pattern keyword
    27  func Pattern(pattern string) Keyword {
    28  	return func(s *Schema) {
    29  		s.Pattern = &pattern
    30  	}
    31  }
    32  
    33  // Format sets the format keyword
    34  func Format(format string) Keyword {
    35  	return func(s *Schema) {
    36  		s.Format = &format
    37  	}
    38  }
    39  
    40  // ===============================
    41  // Number keywords
    42  // ===============================
    43  
    44  // Min sets the minimum keyword
    45  func Min(min float64) Keyword {
    46  	return func(s *Schema) {
    47  		s.Minimum = NewRat(min)
    48  	}
    49  }
    50  
    51  // Max sets the maximum keyword
    52  func Max(max float64) Keyword {
    53  	return func(s *Schema) {
    54  		s.Maximum = NewRat(max)
    55  	}
    56  }
    57  
    58  // ExclusiveMin sets the exclusiveMinimum keyword
    59  func ExclusiveMin(min float64) Keyword {
    60  	return func(s *Schema) {
    61  		s.ExclusiveMinimum = NewRat(min)
    62  	}
    63  }
    64  
    65  // ExclusiveMax sets the exclusiveMaximum keyword
    66  func ExclusiveMax(max float64) Keyword {
    67  	return func(s *Schema) {
    68  		s.ExclusiveMaximum = NewRat(max)
    69  	}
    70  }
    71  
    72  // MultipleOf sets the multipleOf keyword
    73  func MultipleOf(multiple float64) Keyword {
    74  	return func(s *Schema) {
    75  		s.MultipleOf = NewRat(multiple)
    76  	}
    77  }
    78  
    79  // ===============================
    80  // Array keywords
    81  // ===============================
    82  
    83  // Items sets the items keyword
    84  func Items(itemSchema *Schema) Keyword {
    85  	return func(s *Schema) {
    86  		s.Items = itemSchema
    87  	}
    88  }
    89  
    90  // MinItems sets the minItems keyword
    91  func MinItems(min int) Keyword {
    92  	return func(s *Schema) {
    93  		f := float64(min)
    94  		s.MinItems = &f
    95  	}
    96  }
    97  
    98  // MaxItems sets the maxItems keyword
    99  func MaxItems(max int) Keyword {
   100  	return func(s *Schema) {
   101  		f := float64(max)
   102  		s.MaxItems = &f
   103  	}
   104  }
   105  
   106  // UniqueItems sets the uniqueItems keyword
   107  func UniqueItems(unique bool) Keyword {
   108  	return func(s *Schema) {
   109  		s.UniqueItems = &unique
   110  	}
   111  }
   112  
   113  // Contains sets the contains keyword
   114  func Contains(schema *Schema) Keyword {
   115  	return func(s *Schema) {
   116  		s.Contains = schema
   117  	}
   118  }
   119  
   120  // MinContains sets the minContains keyword
   121  func MinContains(min int) Keyword {
   122  	return func(s *Schema) {
   123  		f := float64(min)
   124  		s.MinContains = &f
   125  	}
   126  }
   127  
   128  // MaxContains sets the maxContains keyword
   129  func MaxContains(max int) Keyword {
   130  	return func(s *Schema) {
   131  		f := float64(max)
   132  		s.MaxContains = &f
   133  	}
   134  }
   135  
   136  // PrefixItems sets the prefixItems keyword
   137  func PrefixItems(schemas ...*Schema) Keyword {
   138  	return func(s *Schema) {
   139  		s.PrefixItems = schemas
   140  	}
   141  }
   142  
   143  // UnevaluatedItems sets the unevaluatedItems keyword
   144  func UnevaluatedItems(schema *Schema) Keyword {
   145  	return func(s *Schema) {
   146  		s.UnevaluatedItems = schema
   147  	}
   148  }
   149  
   150  // ===============================
   151  // Object keywords
   152  // ===============================
   153  
   154  // Required sets the required keyword
   155  func Required(fields ...string) Keyword {
   156  	return func(s *Schema) {
   157  		s.Required = fields
   158  	}
   159  }
   160  
   161  // AdditionalProps sets the additionalProperties keyword
   162  func AdditionalProps(allowed bool) Keyword {
   163  	return func(s *Schema) {
   164  		s.AdditionalProperties = &Schema{Boolean: &allowed}
   165  	}
   166  }
   167  
   168  // AdditionalPropsSchema sets the additionalProperties keyword with a schema
   169  func AdditionalPropsSchema(schema *Schema) Keyword {
   170  	return func(s *Schema) {
   171  		s.AdditionalProperties = schema
   172  	}
   173  }
   174  
   175  // MinProps sets the minProperties keyword
   176  func MinProps(min int) Keyword {
   177  	return func(s *Schema) {
   178  		f := float64(min)
   179  		s.MinProperties = &f
   180  	}
   181  }
   182  
   183  // MaxProps sets the maxProperties keyword
   184  func MaxProps(max int) Keyword {
   185  	return func(s *Schema) {
   186  		f := float64(max)
   187  		s.MaxProperties = &f
   188  	}
   189  }
   190  
   191  // PatternProps sets the patternProperties keyword
   192  func PatternProps(patterns map[string]*Schema) Keyword {
   193  	return func(s *Schema) {
   194  		schemaMap := SchemaMap(patterns)
   195  		s.PatternProperties = &schemaMap
   196  	}
   197  }
   198  
   199  // PropertyNames sets the propertyNames keyword
   200  func PropertyNames(schema *Schema) Keyword {
   201  	return func(s *Schema) {
   202  		s.PropertyNames = schema
   203  	}
   204  }
   205  
   206  // UnevaluatedProps sets the unevaluatedProperties keyword
   207  func UnevaluatedProps(schema *Schema) Keyword {
   208  	return func(s *Schema) {
   209  		s.UnevaluatedProperties = schema
   210  	}
   211  }
   212  
   213  // DependentRequired sets the dependentRequired keyword
   214  func DependentRequired(dependencies map[string][]string) Keyword {
   215  	return func(s *Schema) {
   216  		s.DependentRequired = dependencies
   217  	}
   218  }
   219  
   220  // DependentSchemas sets the dependentSchemas keyword
   221  func DependentSchemas(dependencies map[string]*Schema) Keyword {
   222  	return func(s *Schema) {
   223  		s.DependentSchemas = dependencies
   224  	}
   225  }
   226  
   227  // ===============================
   228  // Annotation keywords
   229  // ===============================
   230  
   231  // Title sets the title keyword
   232  func Title(title string) Keyword {
   233  	return func(s *Schema) {
   234  		s.Title = &title
   235  	}
   236  }
   237  
   238  // Description sets the description keyword
   239  func Description(desc string) Keyword {
   240  	return func(s *Schema) {
   241  		s.Description = &desc
   242  	}
   243  }
   244  
   245  // Default sets the default keyword
   246  func Default(value interface{}) Keyword {
   247  	return func(s *Schema) {
   248  		s.Default = value
   249  	}
   250  }
   251  
   252  // Examples sets the examples keyword
   253  func Examples(examples ...interface{}) Keyword {
   254  	return func(s *Schema) {
   255  		s.Examples = examples
   256  	}
   257  }
   258  
   259  // Deprecated sets the deprecated keyword
   260  func Deprecated(deprecated bool) Keyword {
   261  	return func(s *Schema) {
   262  		s.Deprecated = &deprecated
   263  	}
   264  }
   265  
   266  // ReadOnly sets the readOnly keyword
   267  func ReadOnly(readOnly bool) Keyword {
   268  	return func(s *Schema) {
   269  		s.ReadOnly = &readOnly
   270  	}
   271  }
   272  
   273  // WriteOnly sets the writeOnly keyword
   274  func WriteOnly(writeOnly bool) Keyword {
   275  	return func(s *Schema) {
   276  		s.WriteOnly = &writeOnly
   277  	}
   278  }
   279  
   280  // ===============================
   281  // Content keywords
   282  // ===============================
   283  
   284  // ContentEncoding sets the contentEncoding keyword
   285  func ContentEncoding(encoding string) Keyword {
   286  	return func(s *Schema) {
   287  		s.ContentEncoding = &encoding
   288  	}
   289  }
   290  
   291  // ContentMediaType sets the contentMediaType keyword
   292  func ContentMediaType(mediaType string) Keyword {
   293  	return func(s *Schema) {
   294  		s.ContentMediaType = &mediaType
   295  	}
   296  }
   297  
   298  // ContentSchema sets the contentSchema keyword
   299  func ContentSchema(schema *Schema) Keyword {
   300  	return func(s *Schema) {
   301  		s.ContentSchema = schema
   302  	}
   303  }
   304  
   305  // ===============================
   306  // Core identifier keywords
   307  // ===============================
   308  
   309  // ID sets the $id keyword
   310  func ID(id string) Keyword {
   311  	return func(s *Schema) {
   312  		s.ID = id
   313  	}
   314  }
   315  
   316  // SchemaURI sets the $schema keyword
   317  func SchemaURI(schemaURI string) Keyword {
   318  	return func(s *Schema) {
   319  		s.Schema = schemaURI
   320  	}
   321  }
   322  
   323  // Anchor sets the $anchor keyword
   324  func Anchor(anchor string) Keyword {
   325  	return func(s *Schema) {
   326  		s.Anchor = anchor
   327  	}
   328  }
   329  
   330  // DynamicAnchor sets the $dynamicAnchor keyword
   331  func DynamicAnchor(anchor string) Keyword {
   332  	return func(s *Schema) {
   333  		s.DynamicAnchor = anchor
   334  	}
   335  }
   336  
   337  // Defs sets the $defs keyword
   338  func Defs(defs map[string]*Schema) Keyword {
   339  	return func(s *Schema) {
   340  		s.Defs = defs
   341  	}
   342  }
   343  
   344  // ===============================
   345  // Format constants
   346  // ===============================
   347  
   348  const (
   349  	FormatEmail               = "email"
   350  	FormatDateTime            = "date-time"
   351  	FormatDate                = "date"
   352  	FormatTime                = "time"
   353  	FormatURI                 = "uri"
   354  	FormatURIRef              = "uri-reference"
   355  	FormatUUID                = "uuid"
   356  	FormatHostname            = "hostname"
   357  	FormatIPv4                = "ipv4"
   358  	FormatIPv6                = "ipv6"
   359  	FormatRegex               = "regex"
   360  	FormatIdnEmail            = "idn-email"
   361  	FormatIdnHostname         = "idn-hostname"
   362  	FormatIRI                 = "iri"
   363  	FormatIRIRef              = "iri-reference"
   364  	FormatURITemplate         = "uri-template"
   365  	FormatJSONPointer         = "json-pointer"
   366  	FormatRelativeJSONPointer = "relative-json-pointer"
   367  	FormatDuration            = "duration"
   368  )
   369  
   370  // ===============================
   371  // Convenience schema functions
   372  // ===============================
   373  
   374  // Email creates an email format string schema
   375  func Email() *Schema {
   376  	return String(Format(FormatEmail))
   377  }
   378  
   379  // DateTime creates a date-time format string schema
   380  func DateTime() *Schema {
   381  	return String(Format(FormatDateTime))
   382  }
   383  
   384  // Date creates a date format string schema
   385  func Date() *Schema {
   386  	return String(Format(FormatDate))
   387  }
   388  
   389  // Time creates a time format string schema
   390  func Time() *Schema {
   391  	return String(Format(FormatTime))
   392  }
   393  
   394  // URI creates a URI format string schema
   395  func URI() *Schema {
   396  	return String(Format(FormatURI))
   397  }
   398  
   399  // URIRef creates a URI reference format string schema
   400  func URIRef() *Schema {
   401  	return String(Format(FormatURIRef))
   402  }
   403  
   404  // UUID creates a UUID format string schema
   405  func UUID() *Schema {
   406  	return String(Format(FormatUUID))
   407  }
   408  
   409  // Hostname creates a hostname format string schema
   410  func Hostname() *Schema {
   411  	return String(Format(FormatHostname))
   412  }
   413  
   414  // IPv4 creates an IPv4 format string schema
   415  func IPv4() *Schema {
   416  	return String(Format(FormatIPv4))
   417  }
   418  
   419  // IPv6 creates an IPv6 format string schema
   420  func IPv6() *Schema {
   421  	return String(Format(FormatIPv6))
   422  }
   423  
   424  // IdnEmail creates an internationalized email format string schema
   425  func IdnEmail() *Schema {
   426  	return String(Format(FormatIdnEmail))
   427  }
   428  
   429  // IdnHostname creates an internationalized hostname format string schema
   430  func IdnHostname() *Schema {
   431  	return String(Format(FormatIdnHostname))
   432  }
   433  
   434  // IRI creates an IRI format string schema
   435  func IRI() *Schema {
   436  	return String(Format(FormatIRI))
   437  }
   438  
   439  // IRIRef creates an IRI reference format string schema
   440  func IRIRef() *Schema {
   441  	return String(Format(FormatIRIRef))
   442  }
   443  
   444  // URITemplate creates a URI template format string schema
   445  func URITemplate() *Schema {
   446  	return String(Format(FormatURITemplate))
   447  }
   448  
   449  // JSONPointer creates a JSON pointer format string schema
   450  func JSONPointer() *Schema {
   451  	return String(Format(FormatJSONPointer))
   452  }
   453  
   454  // RelativeJSONPointer creates a relative JSON pointer format string schema
   455  func RelativeJSONPointer() *Schema {
   456  	return String(Format(FormatRelativeJSONPointer))
   457  }
   458  
   459  // Duration creates a duration format string schema
   460  func Duration() *Schema {
   461  	return String(Format(FormatDuration))
   462  }
   463  
   464  // Regex creates a regex format string schema
   465  func Regex() *Schema {
   466  	return String(Format(FormatRegex))
   467  }
   468  
   469  // PositiveInt creates a positive integer schema
   470  func PositiveInt() *Schema {
   471  	return Integer(Min(1))
   472  }
   473  
   474  // NonNegativeInt creates a non-negative integer schema
   475  func NonNegativeInt() *Schema {
   476  	return Integer(Min(0))
   477  }
   478  
   479  // NegativeInt creates a negative integer schema
   480  func NegativeInt() *Schema {
   481  	return Integer(Max(-1))
   482  }
   483  
   484  // NonPositiveInt creates a non-positive integer schema
   485  func NonPositiveInt() *Schema {
   486  	return Integer(Max(0))
   487  }