/* Package jsonschema provides json-schema compilation and validation. The schema is compiled against the version specified in "$schema" property. If "$schema" property is missing, it uses latest draft which currently implemented by this library. You can force to use specific draft, when "$schema" is missing, as follows: compiler := jsonschema.NewCompiler() compiler.DefaultDraft(jsonschema.Draft4) This package supports loading json-schema from filePath and fileURL. see examples for usage. */
package jsonschema import ( ) // Schema is the representation of a compiled // jsonschema. type Schema struct { up urlPtr resource *Schema dynamicAnchors map[string]*Schema allPropsEvaluated bool allItemsEvaluated bool numItemsEvaluated int DraftVersion int Location string // type agnostic -- Bool *bool // boolean schema ID string Ref *Schema Anchor string RecursiveRef *Schema RecursiveAnchor bool DynamicRef *DynamicRef DynamicAnchor string // "" if not specified Types *Types Enum *Enum Const *any Not *Schema AllOf []*Schema AnyOf []*Schema OneOf []*Schema If *Schema Then *Schema Else *Schema Format *Format // object -- MaxProperties *int MinProperties *int Required []string PropertyNames *Schema Properties map[string]*Schema PatternProperties map[Regexp]*Schema AdditionalProperties any // nil or bool or *Schema Dependencies map[string]any // value is []string or *Schema DependentRequired map[string][]string DependentSchemas map[string]*Schema UnevaluatedProperties *Schema // array -- MinItems *int MaxItems *int UniqueItems bool Contains *Schema MinContains *int MaxContains *int Items any // nil or []*Schema or *Schema AdditionalItems any // nil or bool or *Schema PrefixItems []*Schema Items2020 *Schema UnevaluatedItems *Schema // string -- MinLength *int MaxLength *int Pattern Regexp ContentEncoding *Decoder ContentMediaType *MediaType ContentSchema *Schema // number -- Maximum *big.Rat Minimum *big.Rat ExclusiveMaximum *big.Rat ExclusiveMinimum *big.Rat MultipleOf *big.Rat Extensions []SchemaExt // annotations -- Title string Description string Default *any Comment string ReadOnly bool WriteOnly bool Examples []any Deprecated bool } // -- type jsonType int const ( invalidType jsonType = 0 nullType jsonType = 1 << iota booleanType numberType integerType stringType arrayType objectType ) func typeOf( any) jsonType { switch v := .(type) { case nil: return nullType case bool: return booleanType case float64: if math.IsNaN() || math.IsInf(, 0) { return invalidType } return numberType case float32: if math.IsNaN(float64()) || math.IsInf(float64(), 0) { return invalidType } return numberType case json.Number, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64: return numberType case string: return stringType case []any: return arrayType case map[string]any: return objectType default: return invalidType } } func typeFromString( string) jsonType { switch { case "null": return nullType case "boolean": return booleanType case "number": return numberType case "integer": return integerType case "string": return stringType case "array": return arrayType case "object": return objectType } return invalidType } func ( jsonType) () string { switch { case nullType: return "null" case booleanType: return "boolean" case numberType: return "number" case integerType: return "integer" case stringType: return "string" case arrayType: return "array" case objectType: return "object" } return "" } // -- // Types encapsulates list of json value types. type Types int func newTypes( any) *Types { var Types switch v := .(type) { case string: .Add() case []any: for , := range { if , := .(string); { .Add() } } } if .IsEmpty() { return nil } return & } func ( Types) () bool { return == 0 } // Add specified json type. If typ is // not valid json type it is ignored. func ( *Types) ( string) { .add(typeFromString()) } func ( *Types) ( jsonType) { * = Types(int(*) | int()) } func ( Types) ( jsonType) bool { return int()&int() != 0 } func ( Types) () []string { := []jsonType{ nullType, booleanType, numberType, integerType, stringType, arrayType, objectType, } var []string for , := range { if .contains() { = append(, .String()) } } return } func ( Types) () string { return fmt.Sprintf("%v", .ToStrings()) } // -- type Enum struct { Values []any types Types } func newEnum( []any) *Enum { var Types for , := range { .add(typeOf()) } return &Enum{, } } // -- type DynamicRef struct { Ref *Schema Anchor string // "" if not specified } func newSchema( urlPtr) *Schema { return &Schema{up: , Location: .String()} }