added custom insertion id regex check

This commit is contained in:
Gani Georgiev 2022-08-11 10:29:01 +03:00
parent ff935a39a1
commit 147344546b
9 changed files with 37 additions and 4 deletions

View File

@ -74,6 +74,7 @@ func (form *AdminUpsert) Validate() error {
validation.When( validation.When(
form.admin.IsNew(), form.admin.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength), validation.Length(models.DefaultIdLength, models.DefaultIdLength),
validation.Match(idRegex),
).Else(validation.In(form.admin.Id)), ).Else(validation.In(form.admin.Id)),
), ),
validation.Field( validation.Field(

View File

@ -412,7 +412,13 @@ func TestAdminUpsertWithCustomId(t *testing.T) {
true, true,
}, },
{ {
"id = 15 chars", "id = 15 chars (invalid chars)",
`{"id":"a@3456789012345"}`,
&models.Admin{},
true,
},
{
"id = 15 chars (valid chars)",
`{"id":"a23456789012345"}`, `{"id":"a23456789012345"}`,
&models.Admin{}, &models.Admin{},
false, false,

View File

@ -2,6 +2,11 @@
// validation and applying changes to existing DB models through the app Dao. // validation and applying changes to existing DB models through the app Dao.
package forms package forms
import "regexp"
// base ID value regex pattern
var idRegex = regexp.MustCompile(`^[^\@\#\$\&\|\.\,\'\"\\\/\s]+$`)
// InterceptorNextFunc is a interceptor handler function. // InterceptorNextFunc is a interceptor handler function.
// Usually used in combination with InterceptorFunc. // Usually used in combination with InterceptorFunc.
type InterceptorNextFunc = func() error type InterceptorNextFunc = func() error

View File

@ -97,6 +97,7 @@ func (form *CollectionUpsert) Validate() error {
validation.When( validation.When(
form.collection.IsNew(), form.collection.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength), validation.Length(models.DefaultIdLength, models.DefaultIdLength),
validation.Match(idRegex),
).Else(validation.In(form.collection.Id)), ).Else(validation.In(form.collection.Id)),
), ),
validation.Field( validation.Field(

View File

@ -590,7 +590,13 @@ func TestCollectionUpsertWithCustomId(t *testing.T) {
true, true,
}, },
{ {
"id = 15 chars", "id = 15 chars (invalid chars)",
`{"id":"a@3456789012345"}`,
newCollection(),
true,
},
{
"id = 15 chars (valid chars)",
`{"id":"a23456789012345"}`, `{"id":"a23456789012345"}`,
newCollection(), newCollection(),
false, false,

View File

@ -276,6 +276,7 @@ func (form *RecordUpsert) Validate() error {
validation.When( validation.When(
form.record.IsNew(), form.record.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength), validation.Length(models.DefaultIdLength, models.DefaultIdLength),
validation.Match(idRegex),
).Else(validation.In(form.record.Id)), ).Else(validation.In(form.record.Id)),
), ),
) )

View File

@ -653,7 +653,13 @@ func TestRecordUpsertWithCustomId(t *testing.T) {
true, true,
}, },
{ {
"id = 15 chars", "id = 15 chars (invalid chars)",
map[string]string{"id": "a@3456789012345"},
models.NewRecord(collection),
true,
},
{
"id = 15 chars (valid chars)",
map[string]string{"id": "a23456789012345"}, map[string]string{"id": "a23456789012345"},
models.NewRecord(collection), models.NewRecord(collection),
false, false,

View File

@ -76,6 +76,7 @@ func (form *UserUpsert) Validate() error {
validation.When( validation.When(
form.user.IsNew(), form.user.IsNew(),
validation.Length(models.DefaultIdLength, models.DefaultIdLength), validation.Length(models.DefaultIdLength, models.DefaultIdLength),
validation.Match(idRegex),
).Else(validation.In(form.user.Id)), ).Else(validation.In(form.user.Id)),
), ),
validation.Field( validation.Field(

View File

@ -369,7 +369,13 @@ func TestUserUpsertWithCustomId(t *testing.T) {
true, true,
}, },
{ {
"id = 15 chars", "id = 15 chars (invalid chars)",
`{"id":"a@3456789012345"}`,
&models.User{},
true,
},
{
"id = 15 chars (valid chars)",
`{"id":"a23456789012345"}`, `{"id":"a23456789012345"}`,
&models.User{}, &models.User{},
false, false,