From 42e9f18c89a36cac4440e5f2bf492369201e72dd Mon Sep 17 00:00:00 2001 From: "qiujunjie.zry" Date: Thu, 30 Nov 2023 21:23:12 +0800 Subject: [PATCH 1/2] upgrade Change-Id: I0a4690eed0ec47bc34d9369c2c0e106b0e51574c --- .gitignore | 1 + go.mod | 11 +++- schema.go | 146 ++++++++++++++++++++--------------------- schemaLoader.go | 10 +-- schemaReferencePool.go | 8 +-- subSchema.go | 63 +++++++++--------- validation.go | 108 +++++++++++++++--------------- 7 files changed, 178 insertions(+), 169 deletions(-) diff --git a/.gitignore b/.gitignore index 68e993ce..a57d9349 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ *.sw[nop] *.iml .vscode/ +.idea/ diff --git a/go.mod b/go.mod index b709d7fc..64aee0f3 100644 --- a/go.mod +++ b/go.mod @@ -1,7 +1,14 @@ -module github.com/xeipuuv/gojsonschema +module github.com/qjjayy/gojsonschema + +go 1.18 require ( github.com/stretchr/testify v1.3.0 - github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 ) + +require ( + github.com/davecgh/go-spew v1.1.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/xeipuuv/gojsonpointer v0.0.0-20180127040702-4e3ac2762d5f // indirect +) diff --git a/schema.go b/schema.go index 9e93cd79..7202a0ac 100644 --- a/schema.go +++ b/schema.go @@ -19,7 +19,7 @@ // repository-name gojsonschema // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language. // -// description Defines Schema, the main entry to every subSchema. +// description Defines Schema, the main entry to every SubSchema. // Contains the parsing logic and error checking. // // created 26-02-2013 @@ -52,35 +52,35 @@ func NewSchema(l JSONLoader) (*Schema, error) { // Schema holds a schema type Schema struct { - documentReference gojsonreference.JsonReference - rootSchema *subSchema - pool *schemaPool - referencePool *schemaReferencePool + DocumentReference gojsonreference.JsonReference + RootSchema *SubSchema + Pool *schemaPool + ReferencePool *schemaReferencePool } func (d *Schema) parse(document interface{}, draft Draft) error { - d.rootSchema = &subSchema{property: STRING_ROOT_SCHEMA_PROPERTY, draft: &draft} - return d.parseSchema(document, d.rootSchema) + d.RootSchema = &SubSchema{Property: STRING_ROOT_SCHEMA_PROPERTY, draft: &draft} + return d.parseSchema(document, d.RootSchema) } // SetRootSchemaName sets the root-schema name func (d *Schema) SetRootSchemaName(name string) { - d.rootSchema.property = name + d.RootSchema.Property = name } -// Parses a subSchema +// Parses a SubSchema // // Pretty long function ( sorry :) )... but pretty straight forward, repetitive and boring // Not much magic involved here, most of the job is to validate the key names and their values, -// then the values are copied into subSchema struct +// then the values are copied into SubSchema struct // -func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) error { +func (d *Schema) parseSchema(documentNode interface{}, currentSchema *SubSchema) error { if currentSchema.draft == nil { - if currentSchema.parent == nil { + if currentSchema.Parent == nil { return errors.New("Draft not set") } - currentSchema.draft = currentSchema.parent.draft + currentSchema.draft = currentSchema.Parent.draft } // As of draft 6 "true" is equivalent to an empty schema "{}" and false equals "{"not":{}}" @@ -101,13 +101,13 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) m := documentNode.(map[string]interface{}) - if currentSchema.parent == nil { - currentSchema.ref = &d.documentReference - currentSchema.id = &d.documentReference + if currentSchema.Parent == nil { + currentSchema.Ref = &d.DocumentReference + currentSchema.Id = &d.DocumentReference } - if currentSchema.id == nil && currentSchema.parent != nil { - currentSchema.id = currentSchema.parent.id + if currentSchema.Id == nil && currentSchema.Parent != nil { + currentSchema.Id = currentSchema.Parent.Id } // In draft 6 the id keyword was renamed to $id @@ -139,14 +139,14 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if err != nil { return err } - if currentSchema == d.rootSchema { - currentSchema.id = &jsonReference + if currentSchema == d.RootSchema { + currentSchema.Id = &jsonReference } else { - ref, err := currentSchema.parent.id.Inherits(jsonReference) + ref, err := currentSchema.Parent.Id.Inherits(jsonReference) if err != nil { return err } - currentSchema.id = ref + currentSchema.Id = ref } } @@ -156,7 +156,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) for _, dv := range m[KEY_DEFINITIONS].(map[string]interface{}) { if isKind(dv, reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_DEFINITIONS, parent: currentSchema} + newSchema := &SubSchema{Property: KEY_DEFINITIONS, Parent: currentSchema} err := d.parseSchema(dv, newSchema) @@ -196,7 +196,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) )) } if k, ok := m[KEY_TITLE].(string); ok { - currentSchema.title = &k + currentSchema.Title = &k } // description @@ -210,7 +210,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) )) } if k, ok := m[KEY_DESCRIPTION].(string); ok { - currentSchema.description = &k + currentSchema.Description = &k } // $ref @@ -231,10 +231,10 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) return err } - currentSchema.ref = &jsonReference + currentSchema.Ref = &jsonReference - if sch, ok := d.referencePool.Get(currentSchema.ref.String()); ok { - currentSchema.refSchema = sch + if sch, ok := d.ReferencePool.Get(currentSchema.Ref.String()); ok { + currentSchema.RefSchema = sch } else { err := d.parseReference(documentNode, currentSchema) @@ -250,7 +250,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_TYPE) { if isKind(m[KEY_TYPE], reflect.String) { if k, ok := m[KEY_TYPE].(string); ok { - err := currentSchema.types.Add(k) + err := currentSchema.Types.Add(k) if err != nil { return err } @@ -268,7 +268,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) }, )) } - if err := currentSchema.types.Add(typeInArray.(string)); err != nil { + if err := currentSchema.Types.Add(typeInArray.(string)); err != nil { return err } } @@ -296,10 +296,10 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) // additionalProperties if existsMapKey(m, KEY_ADDITIONAL_PROPERTIES) { if isKind(m[KEY_ADDITIONAL_PROPERTIES], reflect.Bool) { - currentSchema.additionalProperties = m[KEY_ADDITIONAL_PROPERTIES].(bool) + currentSchema.AdditionalProperties = m[KEY_ADDITIONAL_PROPERTIES].(bool) } else if isKind(m[KEY_ADDITIONAL_PROPERTIES], reflect.Map) { - newSchema := &subSchema{property: KEY_ADDITIONAL_PROPERTIES, parent: currentSchema, ref: currentSchema.ref} - currentSchema.additionalProperties = newSchema + newSchema := &SubSchema{Property: KEY_ADDITIONAL_PROPERTIES, Parent: currentSchema, Ref: currentSchema.Ref} + currentSchema.AdditionalProperties = newSchema err := d.parseSchema(m[KEY_ADDITIONAL_PROPERTIES], newSchema) if err != nil { return errors.New(err.Error()) @@ -320,7 +320,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if isKind(m[KEY_PATTERN_PROPERTIES], reflect.Map) { patternPropertiesMap := m[KEY_PATTERN_PROPERTIES].(map[string]interface{}) if len(patternPropertiesMap) > 0 { - currentSchema.patternProperties = make(map[string]*subSchema) + currentSchema.PatternProperties = make(map[string]*SubSchema) for k, v := range patternPropertiesMap { _, err := regexp.MatchString(k, "") if err != nil { @@ -329,12 +329,12 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) ErrorDetails{"pattern": k}, )) } - newSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: k, Parent: currentSchema, Ref: currentSchema.Ref} err = d.parseSchema(v, newSchema) if err != nil { return errors.New(err.Error()) } - currentSchema.patternProperties[k] = newSchema + currentSchema.PatternProperties[k] = newSchema } } } else { @@ -351,8 +351,8 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) // propertyNames if existsMapKey(m, KEY_PROPERTY_NAMES) && *currentSchema.draft >= Draft6 { if isKind(m[KEY_PROPERTY_NAMES], reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_PROPERTY_NAMES, parent: currentSchema, ref: currentSchema.ref} - currentSchema.propertyNames = newSchema + newSchema := &SubSchema{Property: KEY_PROPERTY_NAMES, Parent: currentSchema, Ref: currentSchema.Ref} + currentSchema.PropertyNames = newSchema err := d.parseSchema(m[KEY_PROPERTY_NAMES], newSchema) if err != nil { return err @@ -381,9 +381,9 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if isKind(m[KEY_ITEMS], reflect.Slice) { for _, itemElement := range m[KEY_ITEMS].([]interface{}) { if isKind(itemElement, reflect.Map, reflect.Bool) { - newSchema := &subSchema{parent: currentSchema, property: KEY_ITEMS} - newSchema.ref = currentSchema.ref - currentSchema.itemsChildren = append(currentSchema.itemsChildren, newSchema) + newSchema := &SubSchema{Parent: currentSchema, Property: KEY_ITEMS} + newSchema.Ref = currentSchema.Ref + currentSchema.ItemsChildren = append(currentSchema.ItemsChildren, newSchema) err := d.parseSchema(itemElement, newSchema) if err != nil { return err @@ -397,17 +397,17 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) }, )) } - currentSchema.itemsChildrenIsSingleSchema = false + currentSchema.ItemsChildrenIsSingleSchema = false } } else if isKind(m[KEY_ITEMS], reflect.Map, reflect.Bool) { - newSchema := &subSchema{parent: currentSchema, property: KEY_ITEMS} - newSchema.ref = currentSchema.ref - currentSchema.itemsChildren = append(currentSchema.itemsChildren, newSchema) + newSchema := &SubSchema{Parent: currentSchema, Property: KEY_ITEMS} + newSchema.Ref = currentSchema.Ref + currentSchema.ItemsChildren = append(currentSchema.ItemsChildren, newSchema) err := d.parseSchema(m[KEY_ITEMS], newSchema) if err != nil { return err } - currentSchema.itemsChildrenIsSingleSchema = true + currentSchema.ItemsChildrenIsSingleSchema = true } else { return errors.New(formatErrorDescription( Locale.InvalidType(), @@ -422,10 +422,10 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) // additionalItems if existsMapKey(m, KEY_ADDITIONAL_ITEMS) { if isKind(m[KEY_ADDITIONAL_ITEMS], reflect.Bool) { - currentSchema.additionalItems = m[KEY_ADDITIONAL_ITEMS].(bool) + currentSchema.AdditionalItems = m[KEY_ADDITIONAL_ITEMS].(bool) } else if isKind(m[KEY_ADDITIONAL_ITEMS], reflect.Map) { - newSchema := &subSchema{property: KEY_ADDITIONAL_ITEMS, parent: currentSchema, ref: currentSchema.ref} - currentSchema.additionalItems = newSchema + newSchema := &SubSchema{Property: KEY_ADDITIONAL_ITEMS, Parent: currentSchema, Ref: currentSchema.Ref} + currentSchema.AdditionalItems = newSchema err := d.parseSchema(m[KEY_ADDITIONAL_ITEMS], newSchema) if err != nil { return errors.New(err.Error()) @@ -799,8 +799,8 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) } if existsMapKey(m, KEY_CONTAINS) && *currentSchema.draft >= Draft6 { - newSchema := &subSchema{property: KEY_CONTAINS, parent: currentSchema, ref: currentSchema.ref} - currentSchema.contains = newSchema + newSchema := &SubSchema{Property: KEY_CONTAINS, Parent: currentSchema, Ref: currentSchema.Ref} + currentSchema.Contains = newSchema err := d.parseSchema(m[KEY_CONTAINS], newSchema) if err != nil { return err @@ -840,12 +840,12 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) } } - // validation : subSchema + // validation : SubSchema if existsMapKey(m, KEY_ONE_OF) { if isKind(m[KEY_ONE_OF], reflect.Slice) { for _, v := range m[KEY_ONE_OF].([]interface{}) { - newSchema := &subSchema{property: KEY_ONE_OF, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_ONE_OF, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema.oneOf = append(currentSchema.oneOf, newSchema) err := d.parseSchema(v, newSchema) if err != nil { @@ -863,7 +863,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_ANY_OF) { if isKind(m[KEY_ANY_OF], reflect.Slice) { for _, v := range m[KEY_ANY_OF].([]interface{}) { - newSchema := &subSchema{property: KEY_ANY_OF, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_ANY_OF, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema.anyOf = append(currentSchema.anyOf, newSchema) err := d.parseSchema(v, newSchema) if err != nil { @@ -881,7 +881,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_ALL_OF) { if isKind(m[KEY_ALL_OF], reflect.Slice) { for _, v := range m[KEY_ALL_OF].([]interface{}) { - newSchema := &subSchema{property: KEY_ALL_OF, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_ALL_OF, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema.allOf = append(currentSchema.allOf, newSchema) err := d.parseSchema(v, newSchema) if err != nil { @@ -898,7 +898,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_NOT) { if isKind(m[KEY_NOT], reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_NOT, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_NOT, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema.not = newSchema err := d.parseSchema(m[KEY_NOT], newSchema) if err != nil { @@ -915,7 +915,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if *currentSchema.draft >= Draft7 { if existsMapKey(m, KEY_IF) { if isKind(m[KEY_IF], reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_IF, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_IF, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema._if = newSchema err := d.parseSchema(m[KEY_IF], newSchema) if err != nil { @@ -931,7 +931,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_THEN) { if isKind(m[KEY_THEN], reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_THEN, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_THEN, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema._then = newSchema err := d.parseSchema(m[KEY_THEN], newSchema) if err != nil { @@ -947,7 +947,7 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) if existsMapKey(m, KEY_ELSE) { if isKind(m[KEY_ELSE], reflect.Map, reflect.Bool) { - newSchema := &subSchema{property: KEY_ELSE, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_ELSE, Parent: currentSchema, Ref: currentSchema.Ref} currentSchema._else = newSchema err := d.parseSchema(m[KEY_ELSE], newSchema) if err != nil { @@ -965,22 +965,22 @@ func (d *Schema) parseSchema(documentNode interface{}, currentSchema *subSchema) return nil } -func (d *Schema) parseReference(documentNode interface{}, currentSchema *subSchema) error { +func (d *Schema) parseReference(documentNode interface{}, currentSchema *SubSchema) error { var ( refdDocumentNode interface{} dsp *schemaPoolDocument err error ) - newSchema := &subSchema{property: KEY_REF, parent: currentSchema, ref: currentSchema.ref} + newSchema := &SubSchema{Property: KEY_REF, Parent: currentSchema, Ref: currentSchema.Ref} - d.referencePool.Add(currentSchema.ref.String(), newSchema) + d.ReferencePool.Add(currentSchema.Ref.String(), newSchema) - dsp, err = d.pool.GetDocument(*currentSchema.ref) + dsp, err = d.Pool.GetDocument(*currentSchema.Ref) if err != nil { return err } - newSchema.id = currentSchema.ref + newSchema.Id = currentSchema.Ref refdDocumentNode = dsp.Document newSchema.draft = dsp.Draft @@ -1001,13 +1001,13 @@ func (d *Schema) parseReference(documentNode interface{}, currentSchema *subSche return err } - currentSchema.refSchema = newSchema + currentSchema.RefSchema = newSchema return nil } -func (d *Schema) parseProperties(documentNode interface{}, currentSchema *subSchema) error { +func (d *Schema) parseProperties(documentNode interface{}, currentSchema *SubSchema) error { if !isKind(documentNode, reflect.Map) { return errors.New(formatErrorDescription( @@ -1019,8 +1019,8 @@ func (d *Schema) parseProperties(documentNode interface{}, currentSchema *subSch m := documentNode.(map[string]interface{}) for k := range m { schemaProperty := k - newSchema := &subSchema{property: schemaProperty, parent: currentSchema, ref: currentSchema.ref} - currentSchema.propertiesChildren = append(currentSchema.propertiesChildren, newSchema) + newSchema := &SubSchema{Property: schemaProperty, Parent: currentSchema, Ref: currentSchema.Ref} + currentSchema.PropertiesChildren = append(currentSchema.PropertiesChildren, newSchema) err := d.parseSchema(m[k], newSchema) if err != nil { return err @@ -1030,7 +1030,7 @@ func (d *Schema) parseProperties(documentNode interface{}, currentSchema *subSch return nil } -func (d *Schema) parseDependencies(documentNode interface{}, currentSchema *subSchema) error { +func (d *Schema) parseDependencies(documentNode interface{}, currentSchema *SubSchema) error { if !isKind(documentNode, reflect.Map) { return errors.New(formatErrorDescription( @@ -1040,7 +1040,7 @@ func (d *Schema) parseDependencies(documentNode interface{}, currentSchema *subS } m := documentNode.(map[string]interface{}) - currentSchema.dependencies = make(map[string]interface{}) + currentSchema.Dependencies = make(map[string]interface{}) for k := range m { switch reflect.ValueOf(m[k]).Kind() { @@ -1060,16 +1060,16 @@ func (d *Schema) parseDependencies(documentNode interface{}, currentSchema *subS )) } valuesToRegister = append(valuesToRegister, value.(string)) - currentSchema.dependencies[k] = valuesToRegister + currentSchema.Dependencies[k] = valuesToRegister } case reflect.Map, reflect.Bool: - depSchema := &subSchema{property: k, parent: currentSchema, ref: currentSchema.ref} + depSchema := &SubSchema{Property: k, Parent: currentSchema, Ref: currentSchema.Ref} err := d.parseSchema(m[k], depSchema) if err != nil { return err } - currentSchema.dependencies[k] = depSchema + currentSchema.Dependencies[k] = depSchema default: return errors.New(formatErrorDescription( diff --git a/schemaLoader.go b/schemaLoader.go index 20db0c1f..2a5e9336 100644 --- a/schemaLoader.go +++ b/schemaLoader.go @@ -153,15 +153,15 @@ func (sl *SchemaLoader) Compile(rootSchema JSONLoader) (*Schema, error) { } d := Schema{} - d.pool = sl.pool - d.pool.jsonLoaderFactory = rootSchema.LoaderFactory() - d.documentReference = ref - d.referencePool = newSchemaReferencePool() + d.Pool = sl.pool + d.Pool.jsonLoaderFactory = rootSchema.LoaderFactory() + d.DocumentReference = ref + d.ReferencePool = newSchemaReferencePool() var doc interface{} if ref.String() != "" { // Get document from schema pool - spd, err := d.pool.GetDocument(d.documentReference) + spd, err := d.Pool.GetDocument(d.DocumentReference) if err != nil { return nil, err } diff --git a/schemaReferencePool.go b/schemaReferencePool.go index 6e5e1b5c..0866bcd5 100644 --- a/schemaReferencePool.go +++ b/schemaReferencePool.go @@ -30,18 +30,18 @@ import ( ) type schemaReferencePool struct { - documents map[string]*subSchema + documents map[string]*SubSchema } func newSchemaReferencePool() *schemaReferencePool { p := &schemaReferencePool{} - p.documents = make(map[string]*subSchema) + p.documents = make(map[string]*SubSchema) return p } -func (p *schemaReferencePool) Get(ref string) (r *subSchema, o bool) { +func (p *schemaReferencePool) Get(ref string) (r *SubSchema, o bool) { if internalLogEnabled { internalLog(fmt.Sprintf("Schema Reference ( %s )", ref)) @@ -57,7 +57,7 @@ func (p *schemaReferencePool) Get(ref string) (r *subSchema, o bool) { return nil, false } -func (p *schemaReferencePool) Add(ref string, sch *subSchema) { +func (p *schemaReferencePool) Add(ref string, sch *SubSchema) { if internalLogEnabled { internalLog(fmt.Sprintf("Add Schema Reference %s to pool", ref)) diff --git a/subSchema.go b/subSchema.go index ec779812..4dd603ea 100644 --- a/subSchema.go +++ b/subSchema.go @@ -19,17 +19,18 @@ // repository-name gojsonschema // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language. // -// description Defines the structure of a sub-subSchema. -// A sub-subSchema can contain other sub-schemas. +// description Defines the structure of a sub-SubSchema. +// A sub-SubSchema can contain other sub-schemas. // // created 27-02-2013 package gojsonschema import ( - "github.com/xeipuuv/gojsonreference" "math/big" "regexp" + + "github.com/xeipuuv/gojsonreference" ) // Constants @@ -76,32 +77,32 @@ const ( KEY_ELSE = "else" ) -type subSchema struct { +type SubSchema struct { draft *Draft - // basic subSchema meta properties - id *gojsonreference.JsonReference - title *string - description *string + // basic SubSchema meta properties + Id *gojsonreference.JsonReference + Title *string + Description *string - property string + Property string // Quick pass/fail for boolean schemas pass *bool - // Types associated with the subSchema - types jsonSchemaType + // Types associated with the SubSchema + Types jsonSchemaType // Reference url - ref *gojsonreference.JsonReference + Ref *gojsonreference.JsonReference // Schema referenced - refSchema *subSchema + RefSchema *SubSchema // hierarchy - parent *subSchema - itemsChildren []*subSchema - itemsChildrenIsSingleSchema bool - propertiesChildren []*subSchema + Parent *SubSchema + ItemsChildren []*SubSchema + ItemsChildrenIsSingleSchema bool + PropertiesChildren []*SubSchema // validation : number / integer multipleOf *big.Rat @@ -121,29 +122,29 @@ type subSchema struct { maxProperties *int required []string - dependencies map[string]interface{} - additionalProperties interface{} - patternProperties map[string]*subSchema - propertyNames *subSchema + Dependencies map[string]interface{} + AdditionalProperties interface{} + PatternProperties map[string]*SubSchema + PropertyNames *SubSchema // validation : array minItems *int maxItems *int uniqueItems bool - contains *subSchema + Contains *SubSchema - additionalItems interface{} + AdditionalItems interface{} // validation : all _const *string //const is a golang keyword enum []string - // validation : subSchema - oneOf []*subSchema - anyOf []*subSchema - allOf []*subSchema - not *subSchema - _if *subSchema // if/else are golang keywords - _then *subSchema - _else *subSchema + // validation : SubSchema + oneOf []*SubSchema + anyOf []*SubSchema + allOf []*SubSchema + not *SubSchema + _if *SubSchema // if/else are golang keywords + _then *SubSchema + _else *SubSchema } diff --git a/validation.go b/validation.go index 9081bd92..ac2f18f9 100644 --- a/validation.go +++ b/validation.go @@ -19,7 +19,7 @@ // repository-name gojsonschema // repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language. // -// description Extends Schema and subSchema, implements the validation phase. +// description Extends Schema and SubSchema, implements the validation phase. // // created 28-02-2013 @@ -57,18 +57,18 @@ func (v *Schema) Validate(l JSONLoader) (*Result, error) { func (v *Schema) validateDocument(root interface{}) *Result { result := &Result{} context := NewJsonContext(STRING_CONTEXT_ROOT, nil) - v.rootSchema.validateRecursive(v.rootSchema, root, result, context) + v.RootSchema.validateRecursive(v.RootSchema, root, result, context) return result } -func (v *subSchema) subValidateWithContext(document interface{}, context *JsonContext) *Result { +func (v *SubSchema) subValidateWithContext(document interface{}, context *JsonContext) *Result { result := &Result{} v.validateRecursive(v, document, result, context) return result } -// Walker function to validate the json recursively against the subSchema -func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) { +// Walker function to validate the json recursively against the SubSchema +func (v *SubSchema) validateRecursive(currentSubSchema *SubSchema, currentNode interface{}, result *Result, context *JsonContext) { if internalLogEnabled { internalLog("validateRecursive %s", context.String()) @@ -89,20 +89,20 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i } // Handle referenced schemas, returns directly when a $ref is found - if currentSubSchema.refSchema != nil { - v.validateRecursive(currentSubSchema.refSchema, currentNode, result, context) + if currentSubSchema.RefSchema != nil { + v.validateRecursive(currentSubSchema.RefSchema, currentNode, result, context) return } // Check for null value if currentNode == nil { - if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_NULL) { + if currentSubSchema.Types.IsTyped() && !currentSubSchema.Types.Contains(TYPE_NULL) { result.addInternalError( new(InvalidTypeError), context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": TYPE_NULL, }, ) @@ -120,9 +120,9 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i isInt := checkJSONInteger(value) - validType := currentSubSchema.types.Contains(TYPE_NUMBER) || (isInt && currentSubSchema.types.Contains(TYPE_INTEGER)) + validType := currentSubSchema.Types.Contains(TYPE_NUMBER) || (isInt && currentSubSchema.Types.Contains(TYPE_INTEGER)) - if currentSubSchema.types.IsTyped() && !validType { + if currentSubSchema.Types.IsTyped() && !validType { givenType := TYPE_INTEGER if !isInt { @@ -134,7 +134,7 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": givenType, }, ) @@ -157,13 +157,13 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i case reflect.Slice: - if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_ARRAY) { + if currentSubSchema.Types.IsTyped() && !currentSubSchema.Types.Contains(TYPE_ARRAY) { result.addInternalError( new(InvalidTypeError), context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": TYPE_ARRAY, }, ) @@ -180,13 +180,13 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i // Map => JSON object case reflect.Map: - if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_OBJECT) { + if currentSubSchema.Types.IsTyped() && !currentSubSchema.Types.Contains(TYPE_OBJECT) { result.addInternalError( new(InvalidTypeError), context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": TYPE_OBJECT, }, ) @@ -203,10 +203,10 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i v.validateObject(currentSubSchema, castCurrentNode, result, context) v.validateCommon(currentSubSchema, castCurrentNode, result, context) - for _, pSchema := range currentSubSchema.propertiesChildren { - nextNode, ok := castCurrentNode[pSchema.property] + for _, pSchema := range currentSubSchema.PropertiesChildren { + nextNode, ok := castCurrentNode[pSchema.Property] if ok { - subContext := NewJsonContext(pSchema.property, context) + subContext := NewJsonContext(pSchema.Property, context) v.validateRecursive(pSchema, nextNode, result, subContext) } } @@ -215,13 +215,13 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i case reflect.Bool: - if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_BOOLEAN) { + if currentSubSchema.Types.IsTyped() && !currentSubSchema.Types.Contains(TYPE_BOOLEAN) { result.addInternalError( new(InvalidTypeError), context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": TYPE_BOOLEAN, }, ) @@ -237,13 +237,13 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i case reflect.String: - if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_STRING) { + if currentSubSchema.Types.IsTyped() && !currentSubSchema.Types.Contains(TYPE_STRING) { result.addInternalError( new(InvalidTypeError), context, currentNode, ErrorDetails{ - "expected": currentSubSchema.types.String(), + "expected": currentSubSchema.Types.String(), "given": TYPE_STRING, }, ) @@ -266,8 +266,8 @@ func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode i result.incrementScore() } -// Different kinds of validation there, subSchema / common / array / object / string... -func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) { +// Different kinds of validation there, SubSchema / common / array / object / string... +func (v *SubSchema) validateSchema(currentSubSchema *SubSchema, currentNode interface{}, result *Result, context *JsonContext) { if internalLogEnabled { internalLog("validateSchema %s", context.String()) @@ -294,7 +294,7 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte result.addInternalError(new(NumberAnyOfError), context, currentNode, ErrorDetails{}) if bestValidationResult != nil { - // add error messages of closest matching subSchema as + // add error messages of closest matching SubSchema as // that's probably the one the user was trying to match result.mergeErrors(bestValidationResult) } @@ -320,7 +320,7 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte result.addInternalError(new(NumberOneOfError), context, currentNode, ErrorDetails{}) if nbValidated == 0 { - // add error messages of closest matching subSchema as + // add error messages of closest matching SubSchema as // that's probably the one the user was trying to match result.mergeErrors(bestValidationResult) } @@ -351,10 +351,10 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte } } - if currentSubSchema.dependencies != nil && len(currentSubSchema.dependencies) > 0 { + if currentSubSchema.Dependencies != nil && len(currentSubSchema.Dependencies) > 0 { if isKind(currentNode, reflect.Map) { for elementKey := range currentNode.(map[string]interface{}) { - if dependency, ok := currentSubSchema.dependencies[elementKey]; ok { + if dependency, ok := currentSubSchema.Dependencies[elementKey]; ok { switch dependency := dependency.(type) { case []string: @@ -369,7 +369,7 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte } } - case *subSchema: + case *SubSchema: dependency.validateRecursive(dependency, currentNode, result, context) } } @@ -398,7 +398,7 @@ func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode inte result.incrementScore() } -func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) { +func (v *SubSchema) validateCommon(currentSubSchema *SubSchema, value interface{}, result *Result, context *JsonContext) { if internalLogEnabled { internalLog("validateCommon %s", context.String()) @@ -455,7 +455,7 @@ func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{ result.incrementScore() } -func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *JsonContext) { +func (v *SubSchema) validateArray(currentSubSchema *SubSchema, value []interface{}, result *Result, context *JsonContext) { if internalLogEnabled { internalLog("validateArray %s", context.String()) @@ -465,21 +465,21 @@ func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface nbValues := len(value) // TODO explain - if currentSubSchema.itemsChildrenIsSingleSchema { + if currentSubSchema.ItemsChildrenIsSingleSchema { for i := range value { subContext := NewJsonContext(strconv.Itoa(i), context) - validationResult := currentSubSchema.itemsChildren[0].subValidateWithContext(value[i], subContext) + validationResult := currentSubSchema.ItemsChildren[0].subValidateWithContext(value[i], subContext) result.mergeErrors(validationResult) } } else { - if currentSubSchema.itemsChildren != nil && len(currentSubSchema.itemsChildren) > 0 { + if currentSubSchema.ItemsChildren != nil && len(currentSubSchema.ItemsChildren) > 0 { - nbItems := len(currentSubSchema.itemsChildren) + nbItems := len(currentSubSchema.ItemsChildren) // while we have both schemas and values, check them against each other for i := 0; i != nbItems && i != nbValues; i++ { subContext := NewJsonContext(strconv.Itoa(i), context) - validationResult := currentSubSchema.itemsChildren[i].subValidateWithContext(value[i], subContext) + validationResult := currentSubSchema.ItemsChildren[i].subValidateWithContext(value[i], subContext) result.mergeErrors(validationResult) } @@ -487,13 +487,13 @@ func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface // we have less schemas than elements in the instance array, // but that might be ok if "additionalItems" is specified. - switch currentSubSchema.additionalItems.(type) { + switch currentSubSchema.AdditionalItems.(type) { case bool: - if !currentSubSchema.additionalItems.(bool) { + if !currentSubSchema.AdditionalItems.(bool) { result.addInternalError(new(ArrayNoAdditionalItemsError), context, value, ErrorDetails{}) } - case *subSchema: - additionalItemSchema := currentSubSchema.additionalItems.(*subSchema) + case *SubSchema: + additionalItemSchema := currentSubSchema.AdditionalItems.(*SubSchema) for i := nbItems; i != nbValues; i++ { subContext := NewJsonContext(strconv.Itoa(i), context) validationResult := additionalItemSchema.subValidateWithContext(value[i], subContext) @@ -548,14 +548,14 @@ func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface // contains: - if currentSubSchema.contains != nil { + if currentSubSchema.Contains != nil { validatedOne := false var bestValidationResult *Result for i, v := range value { subContext := NewJsonContext(strconv.Itoa(i), context) - validationResult := currentSubSchema.contains.subValidateWithContext(v, subContext) + validationResult := currentSubSchema.Contains.subValidateWithContext(v, subContext) if validationResult.Valid() { validatedOne = true break @@ -581,7 +581,7 @@ func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface result.incrementScore() } -func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *JsonContext) { +func (v *SubSchema) validateObject(currentSubSchema *SubSchema, value map[string]interface{}, result *Result, context *JsonContext) { if internalLogEnabled { internalLog("validateObject %s", context.String()) @@ -630,8 +630,8 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string // Check whether this property is described by "properties" found := false - for _, spValue := range currentSubSchema.propertiesChildren { - if pk == spValue.property { + for _, spValue := range currentSubSchema.PropertiesChildren { + if pk == spValue.Property { found = true } } @@ -641,7 +641,7 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string // If it is not described by neither "properties" nor "patternProperties" it must pass "additionalProperties" if !found && !ppMatch { - switch ap := currentSubSchema.additionalProperties.(type) { + switch ap := currentSubSchema.AdditionalProperties.(type) { case bool: // Handle the boolean case separately as it's cleaner to return a specific error than failing to pass the false schema if !ap { @@ -653,7 +653,7 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string ) } - case *subSchema: + case *SubSchema: validationResult := ap.subValidateWithContext(value[pk], NewJsonContext(pk, context)) result.mergeErrors(validationResult) } @@ -661,9 +661,9 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string } // propertyNames: - if currentSubSchema.propertyNames != nil { + if currentSubSchema.PropertyNames != nil { for pk := range value { - validationResult := currentSubSchema.propertyNames.subValidateWithContext(pk, context) + validationResult := currentSubSchema.PropertyNames.subValidateWithContext(pk, context) if !validationResult.Valid() { result.addInternalError(new(InvalidPropertyNameError), context, @@ -678,7 +678,7 @@ func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string result.incrementScore() } -func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *JsonContext) bool { +func (v *SubSchema) validatePatternProperty(currentSubSchema *SubSchema, key string, value interface{}, result *Result, context *JsonContext) bool { if internalLogEnabled { internalLog("validatePatternProperty %s", context.String()) @@ -687,7 +687,7 @@ func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key str validated := false - for pk, pv := range currentSubSchema.patternProperties { + for pk, pv := range currentSubSchema.PatternProperties { if matches, _ := regexp.MatchString(pk, key); matches { validated = true subContext := NewJsonContext(key, context) @@ -704,7 +704,7 @@ func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key str return true } -func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) { +func (v *SubSchema) validateString(currentSubSchema *SubSchema, value interface{}, result *Result, context *JsonContext) { // Ignore JSON numbers if isJSONNumber(value) { @@ -761,7 +761,7 @@ func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{ result.incrementScore() } -func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) { +func (v *SubSchema) validateNumber(currentSubSchema *SubSchema, value interface{}, result *Result, context *JsonContext) { // Ignore non numbers if !isJSONNumber(value) { From 9ce63c22ed27aea8d5faa4935a9ba320c3c18a39 Mon Sep 17 00:00:00 2001 From: "qiujunjie.zry" Date: Thu, 30 Nov 2023 21:56:07 +0800 Subject: [PATCH 2/2] test Change-Id: Id60ece77d150751335b24696d01d0552a31a9db0 --- glide.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/glide.yaml b/glide.yaml index ab6fb867..6665ab8f 100644 --- a/glide.yaml +++ b/glide.yaml @@ -1,7 +1,7 @@ -package: github.com/xeipuuv/gojsonschema +package: github.com/qjjayy/gojsonschema license: Apache 2.0 import: -- package: github.com/xeipuuv/gojsonschema +- package: github.com/qjjayy/gojsonschema - package: github.com/xeipuuv/gojsonpointer