-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathconstructor.go
More file actions
186 lines (155 loc) · 5.11 KB
/
Copy pathconstructor.go
File metadata and controls
186 lines (155 loc) · 5.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package jsonschema
// defaultCompiler is the compiler used by the constructor API.
var defaultCompiler = NewCompiler()
// SetDefaultCompiler sets the compiler used by the constructor API.
//
// This mutates process-global state and is not safe for concurrent use with
// other callers of the constructor API. Prefer creating an explicit
// [NewCompiler] and threading it through your code; this function is
// convenient for scripts and small programs but should be avoided in
// servers, libraries, and tests that run in parallel.
func SetDefaultCompiler(c *Compiler) {
defaultCompiler = c
}
// DefaultCompiler returns the compiler used by the constructor API.
//
// The returned compiler is process-global. In server, library, or test
// contexts where loaders, formats, or default functions differ across
// callers, prefer an explicit [NewCompiler] instead of mutating this one.
func DefaultCompiler() *Compiler {
return defaultCompiler
}
// Property pairs a property name with its schema.
type Property struct {
Name string
Schema *Schema
}
// Prop returns a named property for Object.
func Prop(name string, schema *Schema) Property {
return Property{Name: name, Schema: schema}
}
// Object returns an object schema built from properties and keywords.
func Object(items ...any) *Schema {
schema := &Schema{Type: SchemaType{"object"}}
var properties []Property
var keywords []Keyword
for _, item := range items {
switch v := item.(type) {
case Property:
properties = append(properties, v)
case Keyword:
keywords = append(keywords, v)
}
}
if len(properties) > 0 {
props := make(SchemaMap)
for _, prop := range properties {
props[prop.Name] = prop.Schema
}
schema.Properties = &props
}
for _, keyword := range keywords {
keyword(schema)
}
schema.initializeSchema(nil, nil)
return schema
}
func newTypedSchema(typeName string, keywords []Keyword) *Schema {
schema := &Schema{}
if typeName != "" {
schema.Type = SchemaType{typeName}
}
for _, keyword := range keywords {
keyword(schema)
}
schema.initializeSchema(nil, nil)
return schema
}
// String returns a string schema with the given keywords.
func String(keywords ...Keyword) *Schema { return newTypedSchema("string", keywords) }
// Integer returns an integer schema with the given keywords.
func Integer(keywords ...Keyword) *Schema { return newTypedSchema("integer", keywords) }
// Number returns a number schema with the given keywords.
func Number(keywords ...Keyword) *Schema { return newTypedSchema("number", keywords) }
// Boolean returns a boolean schema with the given keywords.
func Boolean(keywords ...Keyword) *Schema { return newTypedSchema("boolean", keywords) }
// Null returns a null schema with the given keywords.
func Null(keywords ...Keyword) *Schema { return newTypedSchema("null", keywords) }
// Array returns an array schema with the given keywords.
func Array(keywords ...Keyword) *Schema { return newTypedSchema("array", keywords) }
// Any returns a schema with no type restriction.
func Any(keywords ...Keyword) *Schema { return newTypedSchema("", keywords) }
// Const returns a schema with `const` set to value.
func Const(value any) *Schema {
schema := &Schema{
Const: &ConstValue{Value: value, IsSet: true},
}
schema.initializeSchema(nil, nil)
return schema
}
// Enum returns a schema with `enum` set to values.
func Enum(values ...any) *Schema {
schema := &Schema{Enum: values}
schema.initializeSchema(nil, nil)
return schema
}
// OneOf returns a schema with `oneOf` set to schemas.
func OneOf(schemas ...*Schema) *Schema {
schema := &Schema{OneOf: schemas}
schema.initializeSchema(nil, nil)
return schema
}
// AnyOf returns a schema with `anyOf` set to schemas.
func AnyOf(schemas ...*Schema) *Schema {
schema := &Schema{AnyOf: schemas}
schema.initializeSchema(nil, nil)
return schema
}
// AllOf returns a schema with `allOf` set to schemas.
func AllOf(schemas ...*Schema) *Schema {
schema := &Schema{AllOf: schemas}
schema.initializeSchema(nil, nil)
return schema
}
// Not returns a schema with `not` set to schema.
func Not(schema *Schema) *Schema {
result := &Schema{Not: schema}
result.initializeSchema(nil, nil)
return result
}
// If begins an `if`/`then`/`else` schema.
func If(condition *Schema) *ConditionalSchema {
return &ConditionalSchema{condition: condition}
}
// ConditionalSchema builds `if`/`then`/`else` schemas.
type ConditionalSchema struct {
condition *Schema
then *Schema
otherwise *Schema
}
// Then sets the `then` branch.
func (cs *ConditionalSchema) Then(then *Schema) *ConditionalSchema {
cs.then = then
return cs
}
// Else sets the `else` branch and returns the completed schema.
func (cs *ConditionalSchema) Else(otherwise *Schema) *Schema {
cs.otherwise = otherwise
return cs.ToSchema()
}
// ToSchema returns the conditional schema as a regular schema.
func (cs *ConditionalSchema) ToSchema() *Schema {
schema := &Schema{
If: cs.condition,
Then: cs.then,
Else: cs.otherwise,
}
schema.initializeSchema(nil, nil)
return schema
}
// Ref returns a schema with `$ref` set to ref.
func Ref(ref string) *Schema {
schema := &Schema{Ref: ref}
schema.initializeSchema(nil, nil)
return schema
}