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

     1  package jsonschema
     2  
     3  // Default compiler instance for initializing Schema
     4  var defaultCompiler = NewCompiler()
     5  
     6  // SetDefaultCompiler allows setting a custom compiler for the constructor API
     7  func SetDefaultCompiler(c *Compiler) {
     8  	defaultCompiler = c
     9  }
    10  
    11  // GetDefaultCompiler returns the current default compiler
    12  func GetDefaultCompiler() *Compiler {
    13  	return defaultCompiler
    14  }
    15  
    16  // Property represents a Schema property definition
    17  type Property struct {
    18  	Name   string
    19  	Schema *Schema
    20  }
    21  
    22  // Prop creates a property definition
    23  func Prop(name string, schema *Schema) Property {
    24  	return Property{Name: name, Schema: schema}
    25  }
    26  
    27  // Object creates an object Schema with properties and keywords
    28  func Object(items ...interface{}) *Schema {
    29  	schema := &Schema{Type: SchemaType{"object"}}
    30  
    31  	var properties []Property
    32  	var keywords []Keyword
    33  
    34  	// Separate properties and keywords
    35  	for _, item := range items {
    36  		switch v := item.(type) {
    37  		case Property:
    38  			properties = append(properties, v)
    39  		case Keyword:
    40  			keywords = append(keywords, v)
    41  		}
    42  	}
    43  
    44  	// Set properties
    45  	if len(properties) > 0 {
    46  		props := make(SchemaMap)
    47  		for _, prop := range properties {
    48  			props[prop.Name] = prop.Schema
    49  		}
    50  		schema.Properties = &props
    51  	}
    52  
    53  	// Apply keywords
    54  	for _, keyword := range keywords {
    55  		keyword(schema)
    56  	}
    57  
    58  	// Initialize Schema to make it directly usable
    59  	schema.initializeSchema(nil, nil)
    60  	return schema
    61  }
    62  
    63  // String creates a string Schema with validation keywords
    64  func String(keywords ...Keyword) *Schema {
    65  	schema := &Schema{Type: SchemaType{"string"}}
    66  	for _, keyword := range keywords {
    67  		keyword(schema)
    68  	}
    69  	schema.initializeSchema(nil, nil)
    70  	return schema
    71  }
    72  
    73  // Integer creates an integer Schema with validation keywords
    74  func Integer(keywords ...Keyword) *Schema {
    75  	schema := &Schema{Type: SchemaType{"integer"}}
    76  	for _, keyword := range keywords {
    77  		keyword(schema)
    78  	}
    79  	schema.initializeSchema(nil, nil)
    80  	return schema
    81  }
    82  
    83  // Number creates a number Schema with validation keywords
    84  func Number(keywords ...Keyword) *Schema {
    85  	schema := &Schema{Type: SchemaType{"number"}}
    86  	for _, keyword := range keywords {
    87  		keyword(schema)
    88  	}
    89  	schema.initializeSchema(nil, nil)
    90  	return schema
    91  }
    92  
    93  // Boolean creates a boolean Schema
    94  func Boolean(keywords ...Keyword) *Schema {
    95  	schema := &Schema{Type: SchemaType{"boolean"}}
    96  	for _, keyword := range keywords {
    97  		keyword(schema)
    98  	}
    99  	schema.initializeSchema(nil, nil)
   100  	return schema
   101  }
   102  
   103  // Null creates a null Schema
   104  func Null(keywords ...Keyword) *Schema {
   105  	schema := &Schema{Type: SchemaType{"null"}}
   106  	for _, keyword := range keywords {
   107  		keyword(schema)
   108  	}
   109  	schema.initializeSchema(nil, nil)
   110  	return schema
   111  }
   112  
   113  // Array creates an array Schema with validation keywords
   114  func Array(keywords ...Keyword) *Schema {
   115  	schema := &Schema{Type: SchemaType{"array"}}
   116  	for _, keyword := range keywords {
   117  		keyword(schema)
   118  	}
   119  	schema.initializeSchema(nil, nil)
   120  	return schema
   121  }
   122  
   123  // Any creates a Schema without type restriction
   124  func Any(keywords ...Keyword) *Schema {
   125  	schema := &Schema{}
   126  	for _, keyword := range keywords {
   127  		keyword(schema)
   128  	}
   129  	schema.initializeSchema(nil, nil)
   130  	return schema
   131  }
   132  
   133  // Const creates a const Schema
   134  func Const(value interface{}) *Schema {
   135  	schema := &Schema{
   136  		Const: &ConstValue{Value: value, IsSet: true},
   137  	}
   138  	schema.initializeSchema(nil, nil)
   139  	return schema
   140  }
   141  
   142  // Enum creates an enum Schema
   143  func Enum(values ...interface{}) *Schema {
   144  	schema := &Schema{Enum: values}
   145  	schema.initializeSchema(nil, nil)
   146  	return schema
   147  }
   148  
   149  // OneOf creates a oneOf combination Schema
   150  func OneOf(schemas ...*Schema) *Schema {
   151  	schema := &Schema{OneOf: schemas}
   152  	schema.initializeSchema(nil, nil)
   153  	return schema
   154  }
   155  
   156  // AnyOf creates an anyOf combination Schema
   157  func AnyOf(schemas ...*Schema) *Schema {
   158  	schema := &Schema{AnyOf: schemas}
   159  	schema.initializeSchema(nil, nil)
   160  	return schema
   161  }
   162  
   163  // AllOf creates an allOf combination Schema
   164  func AllOf(schemas ...*Schema) *Schema {
   165  	schema := &Schema{AllOf: schemas}
   166  	schema.initializeSchema(nil, nil)
   167  	return schema
   168  }
   169  
   170  // Not creates a not combination Schema
   171  func Not(schema *Schema) *Schema {
   172  	result := &Schema{Not: schema}
   173  	result.initializeSchema(nil, nil)
   174  	return result
   175  }
   176  
   177  // If creates a conditional Schema with if/then/else keywords
   178  func If(condition *Schema) *ConditionalSchema {
   179  	return &ConditionalSchema{condition: condition}
   180  }
   181  
   182  // ConditionalSchema represents a conditional schema for if/then/else logic
   183  type ConditionalSchema struct {
   184  	condition *Schema
   185  	then      *Schema
   186  	otherwise *Schema
   187  }
   188  
   189  // Then sets the then clause of a conditional schema
   190  func (cs *ConditionalSchema) Then(then *Schema) *ConditionalSchema {
   191  	cs.then = then
   192  	return cs
   193  }
   194  
   195  // Else sets the else clause of a conditional schema
   196  func (cs *ConditionalSchema) Else(otherwise *Schema) *Schema {
   197  	cs.otherwise = otherwise
   198  	return cs.ToSchema()
   199  }
   200  
   201  // ToSchema converts a conditional schema to a regular schema
   202  func (cs *ConditionalSchema) ToSchema() *Schema {
   203  	schema := &Schema{
   204  		If:   cs.condition,
   205  		Then: cs.then,
   206  		Else: cs.otherwise,
   207  	}
   208  	schema.initializeSchema(nil, nil)
   209  	return schema
   210  }
   211  
   212  // Ref creates a reference Schema using $ref keyword
   213  func Ref(ref string) *Schema {
   214  	schema := &Schema{Ref: ref}
   215  	schema.initializeSchema(nil, nil)
   216  	return schema
   217  }