added custom insertion id regex check
This commit is contained in:
		
							parent
							
								
									ff935a39a1
								
							
						
					
					
						commit
						147344546b
					
				| 
						 | 
					@ -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(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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)),
 | 
				
			||||||
		),
 | 
							),
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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(
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -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,
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue