|
1 | 1 | package schema
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "regexp" |
4 | 7 | "runtime"
|
5 | 8 | "testing"
|
6 | 9 |
|
7 | 10 | "github.com/arduino/go-paths-helper"
|
| 11 | + "github.com/arduino/go-properties-orderedmap" |
| 12 | + "github.com/ory/jsonschema/v3" |
8 | 13 | "github.com/stretchr/testify/require"
|
9 | 14 | )
|
10 | 15 |
|
11 |
| -func TestPathURI(t *testing.T) { |
| 16 | +var schemasPath *paths.Path |
| 17 | + |
| 18 | +var validMap = map[string]string{ |
| 19 | + "property1": "foo", |
| 20 | + "property2": "bar", |
| 21 | +} |
| 22 | + |
| 23 | +var validPropertiesMap = properties.NewFromHashmap(validMap) |
| 24 | + |
| 25 | +var validSchemaWithReferences *jsonschema.Schema |
| 26 | + |
| 27 | +func init() { |
| 28 | + workingPath, _ := os.Getwd() |
| 29 | + schemasPath = paths.New(workingPath).Join("testdata") |
| 30 | + |
| 31 | + validSchemaWithReferences = Compile( |
| 32 | + "valid-schema-with-references.json", |
| 33 | + []string{ |
| 34 | + "referenced-schema-1.json", |
| 35 | + "referenced-schema-2.json", |
| 36 | + }, |
| 37 | + schemasPath, |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +func TestCompile(t *testing.T) { |
| 42 | + require.Panics(t, func() { |
| 43 | + Compile("valid-schema-with-references.json", []string{"nonexistent.json"}, schemasPath) |
| 44 | + }) |
| 45 | + |
| 46 | + require.Panics(t, func() { |
| 47 | + Compile("valid-schema-with-references.json", []string{"schema-without-id.json"}, schemasPath) |
| 48 | + }) |
| 49 | + |
| 50 | + require.Panics(t, func() { |
| 51 | + Compile("invalid-schema.json", []string{}, schemasPath) |
| 52 | + }) |
| 53 | + |
| 54 | + require.Panics(t, func() { |
| 55 | + Compile("valid-schema-with-references.json", []string{}, schemasPath) |
| 56 | + }) |
| 57 | + |
| 58 | + require.NotPanics(t, func() { |
| 59 | + Compile("valid-schema.json", []string{}, schemasPath) |
| 60 | + }) |
| 61 | + |
| 62 | + require.NotPanics(t, func() { |
| 63 | + Compile( |
| 64 | + "valid-schema-with-references.json", |
| 65 | + []string{ |
| 66 | + "referenced-schema-1.json", |
| 67 | + "referenced-schema-2.json", |
| 68 | + }, |
| 69 | + schemasPath, |
| 70 | + ) |
| 71 | + }) |
| 72 | +} |
| 73 | + |
| 74 | +func TestValidate(t *testing.T) { |
| 75 | + schemaObject := Compile("valid-schema.json", []string{}, schemasPath) |
| 76 | + propertiesMap := properties.NewFromHashmap(validMap) |
| 77 | + validationResult := Validate(propertiesMap, schemaObject, schemasPath) |
| 78 | + require.Nil(t, validationResult) |
| 79 | + |
| 80 | + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 81 | + require.Nil(t, validationResult) |
| 82 | + |
| 83 | + propertiesMap.Set("property1", "a") |
| 84 | + validationResult = Validate(propertiesMap, schemaObject, schemasPath) |
| 85 | + require.Equal(t, "#/property1", validationResult.InstancePtr) |
| 86 | + require.Equal(t, "#/properties/property1/minLength", validationResult.SchemaPtr) |
| 87 | +} |
| 88 | + |
| 89 | +func TestRequiredPropertyMissing(t *testing.T) { |
| 90 | + propertiesMap := properties.NewFromHashmap(validMap) |
| 91 | + validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 92 | + require.False(t, RequiredPropertyMissing("property1", validationResult, schemasPath)) |
| 93 | + |
| 94 | + propertiesMap.Remove("property1") |
| 95 | + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 96 | + require.True(t, RequiredPropertyMissing("property1", validationResult, schemasPath)) |
| 97 | +} |
| 98 | + |
| 99 | +func TestPropertyPatternMismatch(t *testing.T) { |
| 100 | + propertiesMap := properties.NewFromHashmap(validMap) |
| 101 | + validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 102 | + require.False(t, PropertyPatternMismatch("property2", validationResult, schemasPath)) |
| 103 | + |
| 104 | + propertiesMap.Set("property2", "fOo") |
| 105 | + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 106 | + require.True(t, PropertyPatternMismatch("property2", validationResult, schemasPath)) |
| 107 | + |
| 108 | + require.False(t, PropertyPatternMismatch("property1", validationResult, schemasPath)) |
| 109 | +} |
| 110 | + |
| 111 | +func TestValidationErrorMatch(t *testing.T) { |
| 112 | + propertiesMap := properties.NewFromHashmap(validMap) |
| 113 | + validationResult := Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 114 | + require.False(t, ValidationErrorMatch("", "", "", "", validationResult, schemasPath)) |
| 115 | + |
| 116 | + propertiesMap.Set("property2", "fOo") |
| 117 | + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 118 | + require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult, schemasPath)) |
| 119 | + require.False(t, ValidationErrorMatch("^#/property2$", "nomatch", "nomatch", "nomatch", validationResult, schemasPath)) |
| 120 | + require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", "nomatch", "nomatch", validationResult, schemasPath)) |
| 121 | + require.False(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^\^\[a-z\]\+\$$`, "nomatch", validationResult, schemasPath)) |
| 122 | + require.True(t, ValidationErrorMatch("^#/property2$", "/pattern$", `^"\^\[a-z\]\+\$"$`, "", validationResult, schemasPath)) |
| 123 | + require.True(t, ValidationErrorMatch("", "", "", "", validationResult, schemasPath)) |
| 124 | + |
| 125 | + propertiesMap = properties.NewFromHashmap(validMap) |
| 126 | + propertiesMap.Remove("property1") |
| 127 | + validationResult = Validate(propertiesMap, validSchemaWithReferences, schemasPath) |
| 128 | + require.False(t, ValidationErrorMatch("nomatch", "nomatch", "nomatch", "nomatch", validationResult, schemasPath)) |
| 129 | + require.True(t, ValidationErrorMatch("", "", "", "^#/property1$", validationResult, schemasPath)) |
| 130 | +} |
| 131 | + |
| 132 | +func Test_loadReferencedSchema(t *testing.T) { |
| 133 | + compiler := jsonschema.NewCompiler() |
| 134 | + |
| 135 | + require.Error(t, loadReferencedSchema(compiler, "nonexistent.json", schemasPath)) |
| 136 | + require.Error(t, loadReferencedSchema(compiler, "schema-without-id.json", schemasPath)) |
| 137 | + require.Nil(t, loadReferencedSchema(compiler, "referenced-schema-2.json", schemasPath)) |
| 138 | +} |
| 139 | + |
| 140 | +func Test_schemaID(t *testing.T) { |
| 141 | + _, err := schemaID("schema-without-id.json", schemasPath) |
| 142 | + require.NotNil(t, err) |
| 143 | + |
| 144 | + id, err := schemaID("valid-schema.json", schemasPath) |
| 145 | + require.Equal(t, "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/schema-with-references.json", id) |
| 146 | + require.Nil(t, err) |
| 147 | +} |
| 148 | + |
| 149 | +func Test_compile(t *testing.T) { |
| 150 | + compiler := jsonschema.NewCompiler() |
| 151 | + _, err := compile(compiler, "invalid-schema.json", schemasPath) |
| 152 | + require.Error(t, err) |
| 153 | + |
| 154 | + _, err = compile(compiler, "valid-schema.json", schemasPath) |
| 155 | + require.Nil(t, err) |
| 156 | +} |
| 157 | + |
| 158 | +func Test_pathURI(t *testing.T) { |
12 | 159 | switch runtime.GOOS {
|
13 | 160 | case "windows":
|
14 | 161 | require.Equal(t, "file:///c:/foo%20bar", pathURI(paths.New("c:/foo bar")))
|
15 | 162 | default:
|
16 | 163 | require.Equal(t, "file:///foo%20bar", pathURI(paths.New("/foo bar")))
|
17 | 164 | }
|
18 | 165 | }
|
| 166 | + |
| 167 | +func Test_schemaPointerValue(t *testing.T) { |
| 168 | + validationError := jsonschema.ValidationError{ |
| 169 | + SchemaURL: "https://raw.githubusercontent.com/arduino/arduino-check/main/check/checkdata/schema/testdata/referenced-schema-1.json", |
| 170 | + SchemaPtr: "#/definitions/patternObject/pattern", |
| 171 | + } |
| 172 | + |
| 173 | + schemaPointerValueInterface := schemaPointerValue(&validationError, schemasPath) |
| 174 | + fmt.Printf("%T", schemaPointerValueInterface) |
| 175 | + schemaPointerValue, ok := schemaPointerValueInterface.(string) |
| 176 | + require.True(t, ok) |
| 177 | + require.Equal(t, "^[a-z]+$", schemaPointerValue) |
| 178 | +} |
| 179 | + |
| 180 | +func Test_validationErrorContextMatch(t *testing.T) { |
| 181 | + validationError := jsonschema.ValidationError{ |
| 182 | + Context: nil, |
| 183 | + } |
| 184 | + |
| 185 | + require.True(t, validationErrorContextMatch(regexp.MustCompile(".*"), &validationError)) |
| 186 | + require.False(t, validationErrorContextMatch(regexp.MustCompile("foo"), &validationError)) |
| 187 | + |
| 188 | + validationError.Context = &jsonschema.ValidationErrorContextRequired{ |
| 189 | + Missing: []string{ |
| 190 | + "#/foo", |
| 191 | + "#/bar", |
| 192 | + }, |
| 193 | + } |
| 194 | + |
| 195 | + require.True(t, validationErrorContextMatch(regexp.MustCompile("^#/bar$"), &validationError)) |
| 196 | + require.False(t, validationErrorContextMatch(regexp.MustCompile("nomatch"), &validationError)) |
| 197 | +} |
0 commit comments