-
Notifications
You must be signed in to change notification settings - Fork 1
/
ArraySchema.go
57 lines (43 loc) · 1.2 KB
/
ArraySchema.go
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
package presilo
import (
"encoding/json"
"errors"
)
/*
A schema which describes an array.
*/
type ArraySchema struct {
Schema
// TODO: item schema should be able to have more than one type in it.
// mixed types should be automatically set as subclasses of a common ancestor, in languages that support it
Items TypeSchema
MaxItems *int `json:"maxItems"`
MinItems *int `json:"minItems"`
UniqueItems *bool `json:"uniqueItems"`
RawItems *json.RawMessage `json:"items"`
}
func NewArraySchema() *ArraySchema {
ret := new(ArraySchema)
ret.typeCode = SCHEMATYPE_ARRAY
return ret
}
/*
Creates a new integer schema from a byte slice that can be interpreted as json.
*/
func ParseArraySchema(contents []byte, context *SchemaParseContext) (*ArraySchema, error) {
var ret *ArraySchema
var err error
ret = NewArraySchema()
err = json.Unmarshal(contents, &ret)
if err != nil {
return ret, err
}
if ret.RawItems == nil {
return nil, errors.New("Array specified, but no item type given.")
}
ret.Items, err = ParseSchema(*ret.RawItems, "", context)
return ret, err
}
func (this *ArraySchema) HasConstraints() bool {
return this.MaxItems != nil || this.MinItems != nil || this.UniqueItems != nil
}