Skip to content

Commit

Permalink
Support validation for the JSON field name (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alfus authored Jul 3, 2024
1 parent 85dab08 commit af7512f
Show file tree
Hide file tree
Showing 7 changed files with 2,973 additions and 4 deletions.
23 changes: 21 additions & 2 deletions internal/protoschema/jsonschema/jsonschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,35 @@ func (p *jsonSchemaGenerator) generateDefault(result map[string]interface{}, des
result["type"] = jsObject
p.setDescription(desc, result)
var properties = make(map[string]interface{})
var patternProperties = make(map[string]interface{})
for i := range desc.Fields().Len() {
field := desc.Fields().Get(i)
if p.shouldIgnoreField(field) {
continue
}
name := string(field.Name())
properties[name] = p.generateField(field)

// Generate the schema
fieldSchema := p.generateField(field)

// TODO: Add an option to include custom alias.
aliases := make([]string, 0, 1)

// TODO: Optionally make the json name the 'primary' name.
properties[string(field.Name())] = fieldSchema
if field.JSONName() != string(field.Name()) {
aliases = append(aliases, field.JSONName())
}

if len(aliases) > 0 {
pattern := "^(" + strings.Join(aliases, "|") + ")$"
patternProperties[pattern] = fieldSchema
}
}
result["properties"] = properties
result["additionalProperties"] = false
if len(patternProperties) > 0 {
result["patternProperties"] = patternProperties
}
}

func (p *jsonSchemaGenerator) setDescription(desc protoreflect.Descriptor, result map[string]interface{}) {
Expand Down
2 changes: 1 addition & 1 deletion internal/testdata/codegenrequest/input.json

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion internal/testdata/jsonschema-doc/test.TestAllTypes.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit af7512f

Please sign in to comment.