Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
shreyas-goenka committed Aug 20, 2024
1 parent 6d2f882 commit 65f1c75
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 285 deletions.
283 changes: 1 addition & 282 deletions bundle/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,288 +10,7 @@ import (
"github.com/stretchr/testify/require"
)

func TestIntSchema(t *testing.T) {
var elemInt int

type Bae struct{}

typ := reflect.TypeOf(Bae{})
fmt.Println(typ.PkgPath())

expected :=
`{
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
}`

schema, err := New(reflect.TypeOf(elemInt), nil)
require.NoError(t, err)

jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)

t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}

func TestBooleanSchema(t *testing.T) {
var elem bool

expected :=
`{
"anyOf": [
{
"type": "boolean"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
}`

schema, err := New(reflect.TypeOf(elem), nil)
require.NoError(t, err)

jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)

t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}

func TestStringSchema(t *testing.T) {
var elem string

expected :=
`{
"type": "string"
}`

schema, err := New(reflect.TypeOf(elem), nil)
require.NoError(t, err)

jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)

t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}

func TestStructOfPrimitivesSchema(t *testing.T) {
type Foo struct {
IntVal int `json:"int_val"`
Int8Val int8 `json:"int8_val"`
Int16Val int16 `json:"int16_val"`
Int32Val int32 `json:"int32_val"`
Int64Val int64 `json:"int64_val"`

UIntVal uint `json:"uint_val"`
Uint8Val uint8 `json:"uint8_val"`
Uint16Val uint16 `json:"uint16_val"`
Uint32Val uint32 `json:"uint32_val"`
Uint64Val uint64 `json:"uint64_val"`

Float32Val float32 `json:"float32_val"`
Float64Val float64 `json:"float64_val"`

StringVal string `json:"string_val"`

BoolVal bool `json:"bool_val"`
}

elem := Foo{}

schema, err := New(reflect.TypeOf(elem), nil)
assert.NoError(t, err)

jsonSchema, err := json.MarshalIndent(schema, " ", " ")
assert.NoError(t, err)

expected :=
`{
"type": "object",
"properties": {
"bool_val": {
"anyOf": [
{
"type": "boolean"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"float32_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"float64_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"int16_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"int32_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"int64_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"int8_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"int_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"string_val": {
"type": "string"
},
"uint16_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"uint32_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"uint64_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"uint8_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
},
"uint_val": {
"anyOf": [
{
"type": "number"
},
{
"type": "string",
"pattern": "\\$\\{([a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\.[a-zA-Z]+([-_]?[a-zA-Z0-9]+)*(\\[[0-9]+\\])*)*(\\[[0-9]+\\])*)\\}"
}
]
}
},
"additionalProperties": false,
"required": [
"int_val",
"int8_val",
"int16_val",
"int32_val",
"int64_val",
"uint_val",
"uint8_val",
"uint16_val",
"uint32_val",
"uint64_val",
"float32_val",
"float64_val",
"string_val",
"bool_val"
]
}`

t.Log("[DEBUG] actual: ", string(jsonSchema))
t.Log("[DEBUG] expected: ", expected)
assert.Equal(t, expected, string(jsonSchema))
}
// TODO: Add a test that checks the primitive overrides for reference regexs wor.

func TestStructOfStructsSchema(t *testing.T) {
type Bar struct {
Expand Down
13 changes: 10 additions & 3 deletions libs/jsonschema/from_type_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (

func TestFromTypeBasic(t *testing.T) {
type myStruct struct {
V string `json:"v"`
S string `json:"s"`
I int `json:"i"`
}

strRef := "#/$defs/string"
Expand Down Expand Up @@ -58,14 +59,20 @@ func TestFromTypeBasic(t *testing.T) {
"string": Schema{
Type: "string",
},
"int": Schema{
Type: "integer",
},
},
Properties: map[string]*Schema{
"v": {
"s": {
Reference: &strRef,
},
"i": {
Reference: &intRef,
},
},
AdditionalProperties: false,
Required: []string{"v"},
Required: []string{"s", "i"},
},
},
{
Expand Down

0 comments on commit 65f1c75

Please sign in to comment.