Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support more keywords and improve conversion results for complex json schema #216

Merged
merged 8 commits into from
Jan 15, 2024
2 changes: 2 additions & 0 deletions pkg/3rdparty/jsonschema/draft2019_09_keywords.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ func LoadDraft2019_09() {
RegisterKeyword("oneOf", NewOneOf)
RegisterKeyword("not", NewNot)

SetKeywordOrder("allOf", -1)

// object Keywords
RegisterKeyword("properties", NewProperties)
RegisterKeyword("patternProperties", NewPatternProperties)
Expand Down
8 changes: 8 additions & 0 deletions pkg/3rdparty/jsonschema/keywords_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,14 @@ func NewRef() Keyword {
return new(Ref)
}

// ResolveRef attempts to resolve the reference and return the resolved schema
func (r *Ref) ResolveRef(rootSchema *Schema) *Schema {
if r.resolved == nil {
r._resolveRef(context.Background(), NewValidationState(rootSchema))
}
return r.resolved
}

// ValidateKeyword implements the Keyword interface for Ref
func (r *Ref) ValidateKeyword(ctx context.Context, currentState *ValidationState, data interface{}) {
schemaDebug("[Ref] Validating")
Expand Down
30 changes: 15 additions & 15 deletions pkg/3rdparty/jsonschema/keywords_object.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,15 +214,15 @@ func NewPatternProperties() Keyword {
}

type patternSchema struct {
key string
re *regexp.Regexp
schema *Schema
Key string
Re *regexp.Regexp
Schema *Schema
}

// Register implements the Keyword interface for PatternProperties
func (p *PatternProperties) Register(uri string, registry *SchemaRegistry) {
for _, v := range *p {
v.schema.Register(uri, registry)
v.Schema.Register(uri, registry)
}
}

Expand All @@ -239,13 +239,13 @@ func (p *PatternProperties) Resolve(pointer jptr.Pointer, uri string) *Schema {
patProp := &patternSchema{}

for _, v := range *p {
if v.key == *current {
if v.Key == *current {
patProp = &v
break
}
}

return patProp.schema.Resolve(pointer.Tail(), uri)
return patProp.Schema.Resolve(pointer.Tail(), uri)
}

// ValidateKeyword implements the Keyword interface for PatternProperties
Expand All @@ -254,15 +254,15 @@ func (p PatternProperties) ValidateKeyword(ctx context.Context, currentState *Va
if obj, ok := data.(map[string]interface{}); ok {
for key, val := range obj {
for _, ptn := range p {
if ptn.re.Match([]byte(key)) {
if ptn.Re.Match([]byte(key)) {
currentState.SetEvaluatedKey(key)
subState := currentState.NewSubState()
subState.DescendBase("patternProperties", key)
subState.DescendRelative("patternProperties", key)
subState.DescendInstance(key)

subState.Errs = &[]KeyError{}
ptn.schema.ValidateKeyword(ctx, subState, val)
ptn.Schema.ValidateKeyword(ctx, subState, val)
currentState.AddSubErrors(*subState.Errs...)

if subState.IsValid() {
Expand All @@ -277,8 +277,8 @@ func (p PatternProperties) ValidateKeyword(ctx context.Context, currentState *Va
// JSONProp implements the JSONPather for PatternProperties
func (p PatternProperties) JSONProp(name string) interface{} {
for _, pp := range p {
if pp.key == name {
return pp.schema
if pp.Key == name {
return pp.Schema
}
}
return nil
Expand All @@ -288,7 +288,7 @@ func (p PatternProperties) JSONProp(name string) interface{} {
func (p PatternProperties) JSONChildren() (res map[string]JSONPather) {
res = map[string]JSONPather{}
for i, pp := range p {
res[strconv.Itoa(i)] = pp.schema
res[strconv.Itoa(i)] = pp.Schema
}
return
}
Expand All @@ -308,9 +308,9 @@ func (p *PatternProperties) UnmarshalJSON(data []byte) error {
return fmt.Errorf("invalid pattern: %s: %s", key, err.Error())
}
ptn[i] = patternSchema{
key: key,
re: re,
schema: sch,
Key: key,
Re: re,
Schema: sch,
}
i++
}
Expand All @@ -323,7 +323,7 @@ func (p *PatternProperties) UnmarshalJSON(data []byte) error {
func (p PatternProperties) MarshalJSON() ([]byte, error) {
obj := map[string]interface{}{}
for _, prop := range p {
obj[prop.key] = prop.schema
obj[prop.Key] = prop.Schema
}
return json.Marshal(obj)
}
Expand Down
Loading
Loading