Skip to content

Commit

Permalink
fix: bring validation code inhouse
Browse files Browse the repository at this point in the history
  • Loading branch information
dosco committed Dec 20, 2022
1 parent 64659d8 commit 800faf7
Show file tree
Hide file tree
Showing 16 changed files with 2,390 additions and 11 deletions.
6 changes: 3 additions & 3 deletions core/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

"github.com/dosco/graphjin/v2/core/internal/psql"
"github.com/dosco/graphjin/v2/core/internal/qcode"
"github.com/go-playground/validator/v10"
"github.com/dosco/graphjin/v2/core/internal/valid"
)

type queryComp struct {
Expand All @@ -21,7 +21,7 @@ type stmt struct {
role *Role
qc *qcode.QCode
md psql.Metadata
va *validator.Validate
va *valid.Validate
sql string
}

Expand Down Expand Up @@ -127,7 +127,7 @@ func (gj *graphjin) compileQueryForRole(
}
}

st.va = validator.New()
st.va = valid.New()
st.sql = w.String()
return st, nil
}
7 changes: 2 additions & 5 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"github.com/dosco/graphjin/v2/core/internal/psql"
"github.com/dosco/graphjin/v2/core/internal/qcode"
"github.com/dosco/graphjin/v2/core/internal/sdata"
"github.com/go-playground/validator/v10"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
Expand Down Expand Up @@ -412,12 +411,10 @@ func (c *gcontext) validateAndUpdateVars(ctx context.Context, qcomp *queryComp,
}

if qc.Consts != nil {
errs := qcomp.st.va.ValidateMap(vars, qc.Consts)

errs := qcomp.st.va.ValidateMap(ctx, vars, qc.Consts)
if !c.gj.prod && len(errs) != 0 {
for k, v := range errs {
v1 := v.(validator.ValidationErrors)
c.gj.log.Printf("Validation Failed: $%s: %s", k, v1.Error())
c.gj.log.Printf("validation failed: $%s: %s", k, v.Error())
}
}

Expand Down
4 changes: 2 additions & 2 deletions core/insert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,8 @@ func Example_insertInlineWithValidation() {
printJSON(res.Data)
}
// Unordered output:
// Validation Failed: $full_name: Key: '' Error:Field validation for '' failed on the 'required_if' tag
// Validation Failed: $email: Key: '' Error:Field validation for '' failed on the 'email' tag
// validation failed: $full_name: failed on 'required_if'
// validation failed: $email: failed on 'email'
// validation failed
}

Expand Down
30 changes: 30 additions & 0 deletions core/internal/valid/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
*.so

# Folders
_obj
_test
bin

# Architecture specific extensions/prefixes
*.[568vq]
[568vq].out

*.cgo1.go
*.cgo2.c
_cgo_defun.c
_cgo_gotypes.go
_cgo_export.*

_testmain.go

*.exe
*.test
*.prof
*.test
*.out
*.txt
cover.html
README.html
22 changes: 22 additions & 0 deletions core/internal/valid/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
The MIT License (MIT)

Copyright (c) 2015 Dean Karn

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Loading

1 comment on commit 800faf7

@vercel
Copy link

@vercel vercel bot commented on 800faf7 Dec 20, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.