-
Notifications
You must be signed in to change notification settings - Fork 527
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: use json schema instead hard code (#2399)
- Loading branch information
Showing
9 changed files
with
247 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"main": { | ||
"system_config": { | ||
"properties": { | ||
"config_name": { | ||
"maxLength":100, | ||
"minLength":1, | ||
"pattern":"^[a-zA-Z0-9_]+$", | ||
"type":"string" | ||
}, | ||
"desc": { | ||
"maxLength":256, | ||
"type":"string" | ||
}, | ||
"payload": { | ||
"type":"object", | ||
"minProperties":1 | ||
}, | ||
"create_time": { | ||
"type":"integer" | ||
}, | ||
"update_time": { | ||
"type":"integer" | ||
} | ||
}, | ||
"required": [ | ||
"config_name", | ||
"payload" | ||
], | ||
"type":"object" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package conf | ||
|
||
import ( | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func Test_mergeSchema(t *testing.T) { | ||
type args struct { | ||
apisixSchema []byte | ||
customizeSchema []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
wantRes []byte | ||
wantErr bool | ||
wantErrMessage string | ||
}{ | ||
{ | ||
name: "should failed when have duplicates key", | ||
args: args{ | ||
apisixSchema: []byte(`{"main":{"a":1,"b":2},"plugins":{"a":1}}`), | ||
customizeSchema: []byte(`{"main":{"b":1}}`), | ||
}, | ||
wantErr: true, | ||
wantErrMessage: "duplicates key: main.b between schema.json and customize_schema.json", | ||
}, | ||
{ | ||
name: "should success", | ||
args: args{ | ||
apisixSchema: []byte(`{"main":{"a":1,"b":2},"plugins":{"a":1}}`), | ||
customizeSchema: []byte(`{"main":{"c":3}}`), | ||
}, | ||
wantErr: false, | ||
wantRes: []byte(`{"main":{"a":1,"b":2,"c":3},"plugins":{"a":1}}`), | ||
}, | ||
} | ||
for _, tt := range tests { | ||
|
||
t.Run(tt.name, func(t *testing.T) { | ||
var ( | ||
wantMap map[string]interface{} | ||
gotMap map[string]interface{} | ||
) | ||
|
||
got, err := mergeSchema(tt.args.apisixSchema, tt.args.customizeSchema) | ||
if tt.wantErr { | ||
assert.Equal(t, tt.wantErrMessage, err.Error()) | ||
return | ||
} | ||
|
||
assert.NoError(t, err) | ||
err = json.Unmarshal(got, &gotMap) | ||
assert.NoError(t, err) | ||
err = json.Unmarshal(tt.wantRes, &wantMap) | ||
assert.NoError(t, err) | ||
assert.Equal(t, wantMap, gotMap) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"minLength": 10 | ||
"$schema": "http://json-schema.org/draft-04/schema#", | ||
"type": "object", | ||
"properties": { | ||
"name": { | ||
"type": "string", | ||
"minLength": 10 | ||
}, | ||
"email": { | ||
"type": "string", | ||
"maxLength": 10 | ||
}, | ||
"age": { | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
}, | ||
"email": { | ||
"type": "string", | ||
"maxLength": 10 | ||
}, | ||
"age": { | ||
"type": "integer", | ||
"minimum": 0 | ||
} | ||
}, | ||
"additionalProperties": false | ||
"additionalProperties": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters