-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
commit 0385f973dd53deb01d1718936a0d0c1723d94ea6 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Thu Nov 23 23:21:15 2023 -0400 Fixes commit dfb473e Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Tue Nov 21 14:46:55 2023 -0400 Fixed sonar issues commit 0ddfc7b Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Tue Nov 21 14:08:51 2023 -0400 Fixed default value commit 1eae9e5 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 18 21:20:10 2023 -0400 Updated configuration commit 32c8543 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 4 14:45:47 2023 -0300 Added more validators commit cfb3f72 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 4 13:46:47 2023 -0300 Added more validators commit c6b8906 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 4 12:01:24 2023 -0300 Reorganize validators commit b2ef4e4 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 4 11:33:52 2023 -0300 Added more validators commit 837a17e Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sat Nov 4 10:33:20 2023 -0300 Added more validators commit 6eeb941 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Fri Nov 3 10:45:26 2023 -0300 Added more validators commit f49d179 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Thu Nov 2 22:13:48 2023 -0300 Added proxy validator commit 2209fdb Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Mon Oct 30 02:44:49 2023 -0300 Added object path joining commit c94139e Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Mon Oct 30 02:11:09 2023 -0300 Added path validation commit 1b76ca4 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Mon Oct 30 01:28:45 2023 -0300 Added schema validation commit 9ad6465 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sun Oct 29 19:40:03 2023 -0300 Added tests for validators commit 9811a93 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Mon Oct 23 10:20:57 2023 -0300 Added validators draft commit fcea2e7 Author: Evgeny Abramovich <evg.abramovitch@gmail.com> Date: Sun Oct 22 15:39:22 2023 -0300 Created validation draft
- Loading branch information
Showing
53 changed files
with
2,279 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="format" type="CompoundRunConfigurationType"> | ||
<toRun name="gofmt" type="ShConfigurationType" /> | ||
<toRun name="gofumpt" type="ShConfigurationType" /> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
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,12 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="go test uncors" type="GoTestRunConfiguration" factoryName="Go Test" nameIsGenerated="true"> | ||
<module name="uncors" /> | ||
<working_directory value="$PROJECT_DIR$" /> | ||
<kind value="DIRECTORY" /> | ||
<package value="github.com/evg4b/uncors" /> | ||
<directory value="$PROJECT_DIR$" /> | ||
<filePath value="$PROJECT_DIR$" /> | ||
<framework value="gotest" /> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
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,17 @@ | ||
<component name="ProjectRunConfigurationManager"> | ||
<configuration default="false" name="gofmt" type="ShConfigurationType"> | ||
<option name="SCRIPT_TEXT" value="gofmt -l -s -w ." /> | ||
<option name="INDEPENDENT_SCRIPT_PATH" value="true" /> | ||
<option name="SCRIPT_PATH" value="" /> | ||
<option name="SCRIPT_OPTIONS" value="" /> | ||
<option name="INDEPENDENT_SCRIPT_WORKING_DIRECTORY" value="true" /> | ||
<option name="SCRIPT_WORKING_DIRECTORY" value="$PROJECT_DIR$" /> | ||
<option name="INDEPENDENT_INTERPRETER_PATH" value="true" /> | ||
<option name="INTERPRETER_PATH" value="/bin/zsh" /> | ||
<option name="INTERPRETER_OPTIONS" value="" /> | ||
<option name="EXECUTE_IN_TERMINAL" value="true" /> | ||
<option name="EXECUTE_SCRIPT_FILE" value="false" /> | ||
<envs /> | ||
<method v="2" /> | ||
</configuration> | ||
</component> |
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
File renamed without changes.
File renamed without changes.
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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,44 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
|
||
"github.com/evg4b/uncors/internal/helpers" | ||
"github.com/gobuffalo/validate" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
type DirectoryValidator struct { | ||
Field string | ||
Value string | ||
Fs afero.Fs | ||
} | ||
|
||
func (f *DirectoryValidator) IsValid(errors *validate.Errors) { | ||
if f.Value == "" { | ||
errors.Add(f.Field, fmt.Sprintf("%s must not be empty", f.Field)) | ||
|
||
return | ||
} | ||
|
||
helpers.PassedOrOsFs(&f.Fs) | ||
|
||
stat, err := f.Fs.Stat(f.Value) | ||
if err != nil { | ||
switch { | ||
case os.IsNotExist(err): | ||
errors.Add(f.Field, fmt.Sprintf("%s directory does not exist", f.Field)) | ||
case os.IsPermission(err): | ||
errors.Add(f.Field, fmt.Sprintf("%s directory is not accessible", f.Field)) | ||
default: | ||
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field)) | ||
} | ||
|
||
return | ||
} | ||
|
||
if !stat.IsDir() { | ||
errors.Add(f.Field, fmt.Sprintf("%s is not a directory", f.Field)) | ||
} | ||
} |
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,67 @@ | ||
package base_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/evg4b/uncors/internal/config/validators/base" | ||
"github.com/evg4b/uncors/testing/testutils" | ||
"github.com/gobuffalo/validate" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestDirectoryValidator(t *testing.T) { | ||
const field = "test" | ||
const path = "/demo" | ||
|
||
fs := testutils.FsFromMap(t, map[string]string{ | ||
"file.go": "package validators", | ||
}) | ||
testutils.CheckNoError(t, fs.Mkdir(path, 0o755)) | ||
|
||
t.Run("should not register error if file error", func(t *testing.T) { | ||
errors := validate.Validate(&base.DirectoryValidator{ | ||
Field: field, | ||
Value: path, | ||
Fs: fs, | ||
}) | ||
|
||
assert.False(t, errors.HasAny()) | ||
}) | ||
|
||
t.Run("should register error for", func(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
path string | ||
error string | ||
}{ | ||
{ | ||
name: "empty path", | ||
path: "", | ||
error: "test must not be empty", | ||
}, | ||
{ | ||
name: "directory does not exist", | ||
path: "directory_does_not_exist", | ||
error: "test directory does not exist", | ||
}, | ||
{ | ||
name: "file instead of directory", | ||
path: "file.go", | ||
error: "test is not a directory", | ||
}, | ||
} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
errors := validate.Validate(&base.DirectoryValidator{ | ||
Field: field, | ||
Value: test.path, | ||
Fs: fs, | ||
}) | ||
|
||
require.EqualError(t, errors, test.error) | ||
}) | ||
} | ||
}) | ||
} |
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,34 @@ | ||
package base | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
|
||
"github.com/gobuffalo/validate" | ||
) | ||
|
||
type DurationValidator struct { | ||
Field string | ||
Value time.Duration | ||
AllowZero bool | ||
} | ||
|
||
func (d *DurationValidator) IsValid(errors *validate.Errors) { | ||
if d.AllowZero { | ||
d.validateWithZero(errors) | ||
} else { | ||
d.validateWithoutZero(errors) | ||
} | ||
} | ||
|
||
func (d *DurationValidator) validateWithoutZero(errors *validate.Errors) { | ||
if d.Value <= 0 { | ||
errors.Add(d.Field, fmt.Sprintf("%s must be greater than 0", d.Field)) | ||
} | ||
} | ||
|
||
func (d *DurationValidator) validateWithZero(errors *validate.Errors) { | ||
if d.Value < 0 { | ||
errors.Add(d.Field, fmt.Sprintf("%s must be greater than or equal to 0", d.Field)) | ||
} | ||
} |
Oops, something went wrong.