Skip to content

Commit

Permalink
fix: Fix required field
Browse files Browse the repository at this point in the history
Signed-off-by: Oğuzhan Durgun <[email protected]>
  • Loading branch information
oguzhand95 committed Sep 27, 2023
1 parent fd117f7 commit cbef141
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 131 deletions.
8 changes: 4 additions & 4 deletions internal/module/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ import (
func (m *Module) schemaForMap(value pgs.FieldTypeElem, rules *validate.MapRules) jsonschema.Schema {
m.Debug("schemaForMap")
schema := jsonschema.NewObjectSchema()
schema.AdditionalProperties, _ = m.schemaForElement(value, rules.GetValues())
schema.AdditionalProperties = m.schemaForElement(value, rules.GetValues())

if rules != nil {
if rules.GetKeys().GetString_() != nil {
schema.PropertyNames, _ = m.schemaForString(rules.GetKeys().GetString_(), false) // TODO
schema.PropertyNames = m.schemaForString(rules.GetKeys().GetString_())
}

if rules.MaxPairs != nil {
Expand All @@ -35,7 +35,7 @@ func (m *Module) schemaForMap(value pgs.FieldTypeElem, rules *validate.MapRules)
func (m *Module) schemaForRepeated(item pgs.FieldTypeElem, rules *validate.RepeatedRules) jsonschema.Schema {
m.Debug("schemaForRepeated")
schema := jsonschema.NewArraySchema()
schema.Items, _ = m.schemaForElement(item, rules.GetItems())
schema.Items = m.schemaForElement(item, rules.GetItems())

if rules != nil {
if rules.MaxItems != nil {
Expand All @@ -54,7 +54,7 @@ func (m *Module) schemaForRepeated(item pgs.FieldTypeElem, rules *validate.Repea
return schema
}

func (m *Module) schemaForElement(element pgs.FieldTypeElem, constraints *validate.FieldConstraints) (jsonschema.Schema, bool) {
func (m *Module) schemaForElement(element pgs.FieldTypeElem, constraints *validate.FieldConstraints) jsonschema.Schema {
m.Debug("schemaForElement")
if element.IsEmbed() {
return m.schemaForEmbed(element.Embed(), constraints)
Expand Down
28 changes: 10 additions & 18 deletions internal/module/enum.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func (m *Module) defineEnum(enum pgs.Enum) *jsonschema.StringSchema {
return schema
}

func (m *Module) schemaForEnum(enum pgs.Enum, rules *validate.EnumRules) (jsonschema.Schema, bool) {
func (m *Module) schemaForEnum(enum pgs.Enum, rules *validate.EnumRules) jsonschema.Schema {
m.Debug("schemaForEnum")
if rules != nil {
switch {
Expand All @@ -33,51 +33,43 @@ func (m *Module) schemaForEnum(enum pgs.Enum, rules *validate.EnumRules) (jsonsc
}
}

return m.enumRef(enum), false
return m.enumRef(enum)
}

func (m *Module) schemaForEnumConst(enum pgs.Enum, value int32) (*jsonschema.StringSchema, bool) {
func (m *Module) schemaForEnumConst(enum pgs.Enum, value int32) *jsonschema.StringSchema {
m.Debug("schemaForEnumConst")
schema := jsonschema.NewStringSchema()
schema.Const = jsonschema.String(m.lookUpEnumName(enum, value))
return schema, value != 0

return schema
}

func (m *Module) schemaForEnumIn(enum pgs.Enum, values []int32) (*jsonschema.StringSchema, bool) {
func (m *Module) schemaForEnumIn(enum pgs.Enum, values []int32) *jsonschema.StringSchema {
m.Debug("schemaForEnumIn")
schema := jsonschema.NewStringSchema()
required := true

for _, value := range values {
schema.Enum = append(schema.Enum, m.lookUpEnumName(enum, value))
if value == 0 {
required = false
}
}
return schema, required

return schema
}

func (m *Module) schemaForEnumNotIn(enum pgs.Enum, values []int32) (*jsonschema.StringSchema, bool) {
func (m *Module) schemaForEnumNotIn(enum pgs.Enum, values []int32) *jsonschema.StringSchema {
m.Debug("schemaForEnumNotIn")
exclude := make(map[int32]struct{}, len(values))
for _, v := range values {
exclude[v] = struct{}{}
}

schema := jsonschema.NewStringSchema()
required := true

for _, v := range enum.Values() {
value := v.Value()
if _, ok := exclude[value]; !ok {
schema.Enum = append(schema.Enum, v.Name().String())
if value == 0 {
required = false
}
}
}

return schema, required
return schema
}

func (m *Module) lookUpEnumName(enum pgs.Enum, value int32) string {
Expand Down
17 changes: 10 additions & 7 deletions internal/module/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,32 +54,35 @@ func (m *Module) schemaForField(field pgs.Field) (jsonschema.Schema, bool) {
_, err := field.Extension(validate.E_Field, constraints)
m.CheckErr(err, "unable to read validation constraints from field")

required := constraints.Required
if constraints.IgnoreEmpty {
required = false
}

var schema jsonschema.Schema
var required bool
switch {
case field.Type().IsEmbed():
schema, required = m.schemaForEmbed(field.Type().Embed(), constraints)
schema = m.schemaForEmbed(field.Type().Embed(), constraints)
case field.Type().IsEnum():
schema, required = m.schemaForEnum(field.Type().Enum(), constraints.GetEnum())
schema = m.schemaForEnum(field.Type().Enum(), constraints.GetEnum())
case field.Type().IsMap():
schema = m.schemaForMap(field.Type().Element(), constraints.GetMap())
case field.Type().IsRepeated():
schema = m.schemaForRepeated(field.Type().Element(), constraints.GetRepeated())
required = constraints.Required
default:
schema, required = m.schemaForScalar(field.Type().ProtoType(), constraints)
schema = m.schemaForScalar(field.Type().ProtoType(), constraints)
}

return schema, required && !field.InOneOf()
}

func (m *Module) schemaForEmbed(embed pgs.Message, constraints *validate.FieldConstraints) (jsonschema.Schema, bool) {
func (m *Module) schemaForEmbed(embed pgs.Message, constraints *validate.FieldConstraints) jsonschema.Schema {
m.Debug("schemaForEmbed")
if embed.IsWellKnown() {
return m.schemaForWellKnownType(embed.WellKnownType(), constraints)
}

return m.schemaForMessage(embed), false
return m.schemaForMessage(embed)
}

func (m *Module) schemaForMessage(message pgs.Message) jsonschema.Schema {
Expand Down
19 changes: 2 additions & 17 deletions internal/module/numeric.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ type numericRules struct {
IgnoreEmpty bool `json:"ignore_empty,omitempty"`
}

func (m *Module) schemaForNumericScalar(numeric pgs.ProtoType, constraints *validate.FieldConstraints) (jsonschema.Schema, bool) {
func (m *Module) schemaForNumericScalar(numeric pgs.ProtoType, constraints *validate.FieldConstraints) jsonschema.Schema {
m.Debug("schemaForNumericScalar")
required := false
value := m.valueSchemaForNumericScalar(numeric)
schemas := []jsonschema.NonTrivialSchema{m.stringValueSchemaForNumericScalar(numeric, value)}
rules := m.numericRules(numeric, constraints)
Expand All @@ -41,40 +40,26 @@ func (m *Module) schemaForNumericScalar(numeric pgs.ProtoType, constraints *vali
if rules != nil {
if rules.Const != nil {
value.Const = rules.Const
required = !rules.IgnoreEmpty
}

if rules.Gt != nil {
value.ExclusiveMinimum = rules.Gt
if !rules.Gt.IsNegative() {
required = !rules.IgnoreEmpty
}
}

if rules.Gte != nil {
value.Minimum = rules.Gte
if rules.Gte.IsPositive() {
required = !rules.IgnoreEmpty
}
}

if len(rules.In) > 0 {
value.Enum = rules.In
required = !rules.IgnoreEmpty
}

if rules.Lt != nil {
value.ExclusiveMaximum = rules.Lt
if !rules.Lt.IsPositive() {
required = !rules.IgnoreEmpty
}
}

if rules.Lte != nil {
value.Maximum = rules.Lte
if rules.Lte.IsNegative() {
required = !rules.IgnoreEmpty
}
}

if len(rules.NotIn) > 0 {
Expand All @@ -85,7 +70,7 @@ func (m *Module) schemaForNumericScalar(numeric pgs.ProtoType, constraints *vali
}
}

return jsonschema.AllOf(schemas...), required
return jsonschema.AllOf(schemas...)
}

func (m *Module) valueSchemaForNumericScalar(numeric pgs.ProtoType) *jsonschema.NumberSchema {
Expand Down
68 changes: 10 additions & 58 deletions internal/module/scalar.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,40 @@ import (
"github.com/cerbos/protoc-gen-jsonschema/internal/jsonschema"
)

func (m *Module) schemaForScalar(scalar pgs.ProtoType, constraints *validate.FieldConstraints) (jsonschema.Schema, bool) {
func (m *Module) schemaForScalar(scalar pgs.ProtoType, constraints *validate.FieldConstraints) jsonschema.Schema {
m.Debug("schemaForScalar")
if scalar.IsNumeric() {
return m.schemaForNumericScalar(scalar, constraints)
}

ignoreEmpty := false
if constraints != nil {
ignoreEmpty = constraints.IgnoreEmpty
}

switch scalar {
case pgs.BoolT:
return m.schemaForBool(constraints.GetBool())

case pgs.BytesT:
return m.schemaForBytes(constraints.GetBytes(), ignoreEmpty)

return m.schemaForBytes()
case pgs.StringT:
return m.schemaForString(constraints.GetString_(), ignoreEmpty)

return m.schemaForString(constraints.GetString_())
default:
m.Failf("unexpected scalar type %q", scalar)
return nil, false
return nil
}
}

func (m *Module) schemaForBool(rules *validate.BoolRules) (jsonschema.Schema, bool) {
func (m *Module) schemaForBool(rules *validate.BoolRules) jsonschema.Schema {
m.Debug("schemaForBool")
required := false
schema := jsonschema.NewBooleanSchema()

if rules != nil {
if rules.Const != nil {
schema.Const = jsonschema.Boolean(rules.GetConst())
required = true
}
}

return schema, required
return schema
}

func (m *Module) schemaForBytes(rules *validate.BytesRules, ignoreEmpty bool) (jsonschema.Schema, bool) {
func (m *Module) schemaForBytes() jsonschema.Schema {
m.Debug("schemaForBytes")
required := false

standard := jsonschema.NewStringSchema()
standard.Title = "Standard base64 encoding"
Expand All @@ -72,25 +61,11 @@ func (m *Module) schemaForBytes(rules *validate.BytesRules, ignoreEmpty bool) (j

schema := jsonschema.NewStringSchema()
schema.OneOf = []jsonschema.NonTrivialSchema{standard, urlSafe}

if rules != nil {
required = !ignoreEmpty &&
(len(rules.Const) > 0 ||
len(rules.Contains) > 0 ||
len(rules.In) > 0 ||
rules.MinLen != nil ||
rules.Pattern != nil ||
len(rules.Prefix) > 0 ||
len(rules.Suffix) > 0 ||
rules.WellKnown != nil)
}

return schema, required
return schema
}

func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool) (jsonschema.Schema, bool) {
func (m *Module) schemaForString(rules *validate.StringRules) jsonschema.Schema {
m.Debug("schemaForString")
required := false
schema := jsonschema.NewStringSchema()
schemas := []jsonschema.NonTrivialSchema{schema}
var patterns []string
Expand All @@ -99,27 +74,19 @@ func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool)
if rules != nil {
if rules.Const != nil {
schema.Const = jsonschema.String(rules.GetConst())
required = !ignoreEmpty
}

if rules.Contains != nil {
patterns = append(patterns, regexp.QuoteMeta(rules.GetContains()))
required = !ignoreEmpty
}

if len(rules.In) > 0 {
schema.Enum = rules.In
required = !ignoreEmpty
}

if rules.Len != nil {
schema.MaxLength = jsonschema.Size(rules.GetLen())
schema.MinLength = jsonschema.Size(rules.GetLen())
required = !ignoreEmpty
}

if rules.LenBytes != nil || rules.MinBytes != nil {
required = !ignoreEmpty
}

if rules.MaxLen != nil {
Expand All @@ -128,7 +95,6 @@ func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool)

if rules.MinLen != nil {
schema.MinLength = jsonschema.Size(rules.GetMinLen())
required = !ignoreEmpty
}

if rules.NotContains != nil {
Expand All @@ -145,19 +111,14 @@ func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool)

if rules.Pattern != nil {
patterns = append(patterns, m.makeRegexpCompatibleWithECMAScript(rules.GetPattern()))
if !m.matchesEmptyString(rules.GetPattern()) {
required = !ignoreEmpty
}
}

if rules.Prefix != nil {
patterns = append(patterns, "^"+regexp.QuoteMeta(rules.GetPrefix()))
required = !ignoreEmpty
}

if rules.Suffix != nil {
patterns = append(patterns, regexp.QuoteMeta(rules.GetSuffix())+"$")
required = !ignoreEmpty
}

if rules.WellKnown != nil {
Expand Down Expand Up @@ -186,8 +147,6 @@ func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool)
case *validate.StringRules_UriRef:
schema.Format = jsonschema.StringFormatURIReference
}

required = !ignoreEmpty
}
}

Expand All @@ -201,7 +160,7 @@ func (m *Module) schemaForString(rules *validate.StringRules, ignoreEmpty bool)
}
}

return jsonschema.AllOf(schemas...), required
return jsonschema.AllOf(schemas...)
}

func (m *Module) schemaForStringFormats(formats ...jsonschema.StringFormat) jsonschema.NonTrivialSchema {
Expand Down Expand Up @@ -294,10 +253,3 @@ func writeECMAScriptCompatibleRegexp(w io.StringWriter, expression *syntax.Regex
w.WriteString(expression.String()) //nolint:errcheck
}
}

func (m *Module) matchesEmptyString(pattern string) bool {
m.Debug("matchesEmptyString")
match, err := regexp.MatchString(pattern, "")
m.CheckErr(err, "failed to check if pattern matches empty string")
return match
}
Loading

0 comments on commit cbef141

Please sign in to comment.