Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
yajsv - Yet Another Json Schema Validator
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
SmartDataLab
public
CI-CD tools
yajsv - Yet Another Json Schema Validator
Commits
7aea95e4
Commit
7aea95e4
authored
5 years ago
by
Ken Brooks
Browse files
Options
Downloads
Plain Diff
merged changes from upstream master
parents
e17a5ddd
4ab3c311
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
main.go
+20
-24
20 additions, 24 deletions
main.go
main_test.go
+81
-107
81 additions, 107 deletions
main_test.go
with
101 additions
and
131 deletions
main.go
+
20
−
24
View file @
7aea95e4
...
...
@@ -6,6 +6,7 @@ import (
"bufio"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
...
...
@@ -20,7 +21,7 @@ import (
)
var
(
version
=
"
undefined
"
version
=
"
v1.3.0-dev
"
schemaFlag
=
flag
.
String
(
"s"
,
""
,
"primary JSON schema to validate against, required"
)
quietFlag
=
flag
.
Bool
(
"q"
,
false
,
"quiet, only print validation failures and errors"
)
versionFlag
=
flag
.
Bool
(
"v"
,
false
,
"print version and exit"
)
...
...
@@ -37,13 +38,13 @@ func init() {
func
main
()
{
log
.
SetFlags
(
0
)
os
.
Exit
(
realMain
(
os
.
Args
[
1
:
]))
os
.
Exit
(
realMain
(
os
.
Args
[
1
:
]
,
os
.
Stdout
))
}
func
realMain
(
args
[]
string
)
int
{
func
realMain
(
args
[]
string
,
w
io
.
Writer
)
int
{
flag
.
CommandLine
.
Parse
(
args
)
if
*
versionFlag
{
fmt
.
P
rintln
(
version
)
fmt
.
Fp
rintln
(
w
,
version
)
return
0
}
if
*
schemaFlag
==
""
{
...
...
@@ -70,7 +71,6 @@ func realMain(args []string) int {
if
!
filepath
.
IsAbs
(
pattern
)
{
pattern
=
filepath
.
Join
(
dir
,
pattern
)
}
docs
=
append
(
docs
,
glob
(
pattern
)
...
)
}
if
err
:=
scanner
.
Err
();
err
!=
nil
{
...
...
@@ -89,9 +89,9 @@ func realMain(args []string) int {
}
for
_
,
ref
:=
range
refFlags
{
for
_
,
p
:=
range
glob
(
ref
)
{
absPath
,
absPathE
rr
:=
filepath
.
Abs
(
p
)
if
absPathE
rr
!=
nil
{
log
.
Fatalf
(
"%s: unable to convert to absolute path: %s
\n
"
,
absPath
,
absPathE
rr
)
absPath
,
e
rr
:=
filepath
.
Abs
(
p
)
if
e
rr
!=
nil
{
log
.
Fatalf
(
"%s: unable to convert to absolute path: %s
\n
"
,
absPath
,
e
rr
)
}
if
absPath
==
schemaPath
{
...
...
@@ -125,17 +125,17 @@ func realMain(args []string) int {
failures
:=
make
([]
string
,
0
)
errors
:=
make
([]
string
,
0
)
for
_
,
p
:=
range
docs
{
//fmt.Println(p)
wg
.
Add
(
1
)
go
func
(
path
string
)
{
defer
wg
.
Done
()
sem
<-
0
defer
func
()
{
<-
sem
}()
loader
,
err
:=
jsonLoader
(
path
)
if
err
!=
nil
{
msg
:=
fmt
.
Sprintf
(
"%s: error: load doc %s
\n
"
,
path
,
err
)
fmt
.
P
rintln
(
msg
)
msg
:=
fmt
.
Sprintf
(
"%s: error: load doc
:
%s"
,
path
,
err
)
fmt
.
Fp
rintln
(
w
,
msg
)
errors
=
append
(
errors
,
msg
)
return
}
...
...
@@ -143,7 +143,7 @@ func realMain(args []string) int {
switch
{
case
err
!=
nil
:
msg
:=
fmt
.
Sprintf
(
"%s: error: validate: %s"
,
path
,
err
)
fmt
.
P
rintln
(
msg
)
fmt
.
Fp
rintln
(
w
,
msg
)
errors
=
append
(
errors
,
msg
)
case
!
result
.
Valid
()
:
...
...
@@ -152,11 +152,11 @@ func realMain(args []string) int {
lines
[
i
]
=
fmt
.
Sprintf
(
"%s: fail: %s"
,
path
,
desc
)
}
msg
:=
strings
.
Join
(
lines
,
"
\n
"
)
fmt
.
P
rintln
(
msg
)
fmt
.
Fp
rintln
(
w
,
msg
)
failures
=
append
(
failures
,
msg
)
case
!*
quietFlag
:
fmt
.
P
rintf
(
"%s: pass
\n
"
,
path
)
fmt
.
Fp
rintf
(
w
,
"%s: pass
\n
"
,
path
)
}
}(
p
)
}
...
...
@@ -165,12 +165,12 @@ func realMain(args []string) int {
// Summarize results (e.g. errors)
if
!*
quietFlag
{
if
len
(
failures
)
>
0
{
fmt
.
P
rintf
(
"%d of %d failed validation
\n
"
,
len
(
failures
),
len
(
docs
))
fmt
.
P
rintln
(
strings
.
Join
(
failures
,
"
\n
"
))
fmt
.
Fp
rintf
(
w
,
"%d of %d failed validation
\n
"
,
len
(
failures
),
len
(
docs
))
fmt
.
Fp
rintln
(
w
,
strings
.
Join
(
failures
,
"
\n
"
))
}
if
len
(
errors
)
>
0
{
fmt
.
P
rintf
(
"%d of %d malformed documents
\n
"
,
len
(
errors
),
len
(
docs
))
fmt
.
P
rintln
(
strings
.
Join
(
errors
,
"
\n
"
))
fmt
.
Fp
rintf
(
w
,
"%d of %d malformed documents
\n
"
,
len
(
errors
),
len
(
docs
))
fmt
.
Fp
rintln
(
w
,
strings
.
Join
(
errors
,
"
\n
"
))
}
}
exit
:=
0
...
...
@@ -234,18 +234,14 @@ func glob(pattern string) []string {
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
universalPaths
:=
make
([]
string
,
0
)
paths
,
err
:=
filepath
.
Glob
(
pattern
)
for
_
,
mypath
:=
range
paths
{
universalPaths
=
append
(
universalPaths
,
filepath
.
ToSlash
(
mypath
))
}
if
err
!=
nil
{
log
.
Fatal
(
err
)
}
if
len
(
universalP
aths
)
==
0
{
if
len
(
p
aths
)
==
0
{
log
.
Fatalf
(
"%s: no such file or directory"
,
pattern
)
}
return
universalP
aths
return
p
aths
}
type
stringFlags
[]
string
...
...
This diff is collapsed.
Click to expand it.
main_test.go
+
81
−
107
View file @
7aea95e4
// +build windows !windows
package
main
import
(
"log"
"path/filepath"
"sort"
"strings"
"testing"
)
func
ExampleMain_pass_ymlschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.yml"
,
"testdata/data-pass.yml"
})
if
exit
!=
0
{
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
}
// Output:
// testdata/data-pass.yml: pass
}
func
ExampleMain_pass_jsonschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.json"
,
"testdata/data-pass.yml"
})
if
exit
!=
0
{
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
}
// Output:
// testdata/data-pass.yml: pass
}
func
ExampleMain_pass_jsonschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.json"
,
"testdata/data-pass.json"
})
if
exit
!=
0
{
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
}
// Output:
// testdata/data-pass.json: pass
}
func
ExampleMain_pass_ymlschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-s"
,
"testdata/schema.yml"
,
"testdata/data-pass.json"
})
if
exit
!=
0
{
log
.
Fatalf
(
"exit: got %d, want 0"
,
exit
)
}
// Output:
// testdata/data-pass.json: pass
}
func
ExampleMain_fail_ymlschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-fail.yml"
})
if
exit
!=
1
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
}
// Output:
// testdata/data-fail.yml: fail: (root): foo is required
}
func
ExampleMain_fail_jsonschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-fail.yml"
})
if
exit
!=
1
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
}
// Output:
// testdata/data-fail.yml: fail: (root): foo is required
}
func
ExampleMain_fail_jsonschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-fail.json"
})
if
exit
!=
1
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
}
// Output:
// testdata/data-fail.json: fail: (root): foo is required
}
func
ExampleMain_fail_ymlschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-fail.json"
})
if
exit
!=
1
{
log
.
Fatalf
(
"exit: got %d, want 1"
,
exit
)
}
// Output:
// testdata/data-fail.json: fail: (root): foo is required
func
TestMain
(
t
*
testing
.
T
)
{
tests
:=
[]
struct
{
in
string
out
[]
string
exit
int
}
{
{
"-s testdata/schema.yml testdata/data-pass.yml"
,
[]
string
{
"testdata/data-pass.yml: pass"
},
0
,
},
{
"-s testdata/schema.json testdata/data-pass.yml"
,
[]
string
{
"testdata/data-pass.yml: pass"
},
0
,
},
{
"-s testdata/schema.json testdata/data-pass.json"
,
[]
string
{
"testdata/data-pass.json: pass"
},
0
,
},
{
"-s testdata/schema.yml testdata/data-pass.json"
,
[]
string
{
"testdata/data-pass.json: pass"
},
0
,
},
{
"-q -s testdata/schema.yml testdata/data-fail.yml"
,
[]
string
{
"testdata/data-fail.yml: fail: (root): foo is required"
},
1
,
},
{
"-q -s testdata/schema.json testdata/data-fail.yml"
,
[]
string
{
"testdata/data-fail.yml: fail: (root): foo is required"
},
1
,
},
{
"-q -s testdata/schema.json testdata/data-fail.json"
,
[]
string
{
"testdata/data-fail.json: fail: (root): foo is required"
},
1
,
},
{
"-q -s testdata/schema.yml testdata/data-fail.json"
,
[]
string
{
"testdata/data-fail.json: fail: (root): foo is required"
},
1
,
},
{
"-q -s testdata/schema.json testdata/data-error.json"
,
[]
string
{
"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"
},
2
,
},
{
"-q -s testdata/schema.yml testdata/data-error.yml"
,
[]
string
{
"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream"
},
2
,
},
{
"-q -s testdata/schema.json testdata/data-*.json"
,
[]
string
{
"testdata/data-fail.json: fail: (root): foo is required"
,
"testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')"
,
},
3
,
},
{
"-q -s testdata/schema.yml testdata/data-*.yml"
,
[]
string
{
"testdata/data-error.yml: error: load doc: yaml: found unexpected end of stream"
,
"testdata/data-fail.yml: fail: (root): foo is required"
,
},
3
,
},
}
func
ExampleMain_error_jsonschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-error.json"
})
if
exit
!=
2
{
log
.
Fatalf
(
"exit: got %d, want 2"
,
exit
)
}
// Output:
// testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')
}
for
_
,
tt
:=
range
tests
{
in
:=
strings
.
ReplaceAll
(
tt
.
in
,
"/"
,
string
(
filepath
.
Separator
))
sort
.
Strings
(
tt
.
out
)
out
:=
strings
.
Join
(
tt
.
out
,
"
\n
"
)
out
=
strings
.
ReplaceAll
(
out
,
"/"
,
string
(
filepath
.
Separator
))
func
ExampleMain_error_ymlschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-error.yml"
})
if
exit
!=
2
{
log
.
Fatalf
(
"exit: got %d, want 2"
,
exit
)
t
.
Run
(
in
,
func
(
t
*
testing
.
T
)
{
var
w
strings
.
Builder
exit
:=
realMain
(
strings
.
Split
(
in
,
" "
),
&
w
)
if
exit
!=
tt
.
exit
{
t
.
Fatalf
(
"exit: got %d, want %d"
,
exit
,
tt
.
exit
)
}
// Output:
// testdata/data-error.yml: error: load doc yaml: found unexpected end of stream
lines
:=
strings
.
Split
(
w
.
String
(),
"
\n
"
)
sort
.
Strings
(
lines
)
got
:=
strings
.
Join
(
lines
[
1
:
],
"
\n
"
)
if
got
!=
out
{
t
.
Errorf
(
"got
\n
%s
\n
want
\n
%s"
,
got
,
out
)
}
func
ExampleMain_glob_jsonschema_jsondoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.json"
,
"testdata/data-*.json"
})
if
exit
!=
3
{
log
.
Fatalf
(
"exit: got %d, want 3"
,
exit
)
})
}
// Unordered output:
// testdata/data-error.json: error: validate: invalid character 'o' in literal null (expecting 'u')
// testdata/data-fail.json: fail: (root): foo is required
}
func
ExampleMain_glob_ymlschema_ymldoc
()
{
exit
:=
realMain
([]
string
{
"-q"
,
"-s"
,
"testdata/schema.yml"
,
"testdata/data-*.yml"
})
if
exit
!=
3
{
log
.
Fatalf
(
"exit: got %d, want 3"
,
exit
)
}
// Unordered output:
// testdata/data-fail.yml: fail: (root): foo is required
//
// testdata/data-error.yml: error: load doc yaml: found unexpected end of stream
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment