diff --git a/.gitignore b/.gitignore index 9ee486a7f3ff15d15f4755c04a7746714b4e062d..e17055d92e8013bd8e77386ec18603dae0248dbd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ yajsv -build/ \ No newline at end of file +build/ +coverage.out \ No newline at end of file diff --git a/main.go b/main.go index ff18144381027da7ef5daf1a6c2e6eb62998e04b..f7f2f31490bb3e895ab73a2bdca33ba9cb67ab31 100644 --- a/main.go +++ b/main.go @@ -81,7 +81,7 @@ func realMain(args []string) int { } } if len(docs) == 0 { - return usageError("no JSON documents to validate") + return usageError("no documents to validate") } // Compile target schema @@ -129,26 +129,29 @@ func realMain(args []string) int { loader, err := jsonLoader(path) if err != nil { - log.Fatalf("%s: unable to load doc: %s\n", *schemaFlag, err) - } - result, err := schema.Validate(loader) - switch { - case err != nil: - msg := fmt.Sprintf("%s: error: %s", path, err) + msg := fmt.Sprintf("%s: unable to load doc: %s\n", *schemaFlag, err) fmt.Println(msg) errors = append(errors, msg) - - case !result.Valid(): - lines := make([]string, len(result.Errors())) - for i, desc := range result.Errors() { - lines[i] = fmt.Sprintf("%s: fail: %s", path, desc) + } else { + result, err := schema.Validate(loader) + switch { + case err != nil: + msg := fmt.Sprintf("%s: error: %s", path, err) + fmt.Println(msg) + errors = append(errors, msg) + + case !result.Valid(): + lines := make([]string, len(result.Errors())) + for i, desc := range result.Errors() { + lines[i] = fmt.Sprintf("%s: fail: %s", path, desc) + } + msg := strings.Join(lines, "\n") + fmt.Println(msg) + failures = append(failures, msg) + + case !*quietFlag: + fmt.Printf("%s: pass\n", path) } - msg := strings.Join(lines, "\n") - fmt.Println(msg) - failures = append(failures, msg) - - case !*quietFlag: - fmt.Printf("%s: pass\n", path) } }(p) } diff --git a/main_windows_test.go b/main_windows_test.go index 2d25c02c64662ab57ebc629e14d966647e1ee269..8914a5e705430b6eb6d64225fd1411d97655dd38 100644 --- a/main_windows_test.go +++ b/main_windows_test.go @@ -6,7 +6,25 @@ import ( "log" ) -func ExampleMain_pass() { +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) @@ -15,7 +33,34 @@ func ExampleMain_pass() { // testdata\data-pass.json: pass } -func ExampleMain_fail() { +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) @@ -24,7 +69,16 @@ func ExampleMain_fail() { // testdata\data-fail.json: fail: (root): foo is required } -func ExampleMain_error() { +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 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) @@ -33,7 +87,16 @@ func ExampleMain_error() { // testdata\data-error.json: error: invalid character 'o' in literal null (expecting 'u') } -func ExampleMain_glob() { +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) + } + // Output: + // testdata\schema.yml: unable to load doc: yaml: found unexpected end of stream +} + +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) @@ -42,3 +105,14 @@ func ExampleMain_glob() { // testdata\data-error.json: error: 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\schema.yml: unable to load doc: yaml: found unexpected end of stream + // + // testdata\data-fail.yml: fail: (root): foo is required +} diff --git a/testdata/data-error.yml b/testdata/data-error.yml new file mode 100644 index 0000000000000000000000000000000000000000..0370438aea1d328d1285a880a63f00a436004829 --- /dev/null +++ b/testdata/data-error.yml @@ -0,0 +1 @@ +invalid: "an escaped \' single quote is not valid yaml \ No newline at end of file diff --git a/testdata/data-fail.yml b/testdata/data-fail.yml new file mode 100644 index 0000000000000000000000000000000000000000..5f2b215eed91feeeba334f55ca29a3198f404df5 --- /dev/null +++ b/testdata/data-fail.yml @@ -0,0 +1,2 @@ +--- +bar: missing foo diff --git a/testdata/data-pass.yml b/testdata/data-pass.yml new file mode 100644 index 0000000000000000000000000000000000000000..73761e387a2a94053a53efc8d363e7d473d4e9c3 --- /dev/null +++ b/testdata/data-pass.yml @@ -0,0 +1,3 @@ +--- +foo: asdf +bar: zxcv diff --git a/testdata/schema.yml b/testdata/schema.yml new file mode 100644 index 0000000000000000000000000000000000000000..2d1e4c9606d87911a17f517e264f847e425c4cef --- /dev/null +++ b/testdata/schema.yml @@ -0,0 +1,7 @@ +--- +properties: + foo: + type: string + bar: {} +required: + - foo