Skip to content

Commit

Permalink
Addressing review comments
Browse files Browse the repository at this point in the history
* Merging `jsonschema.is_valid` into `jsonschema.verify`, and `json.is_match_schema` into `json.match_schema`
* Adding yaml test cases in testdata

Signed-off-by: Johan Fylling <johan.dev@fylling.se>
  • Loading branch information
johanfylling committed Feb 25, 2023
1 parent 8f4d04c commit 0501498
Show file tree
Hide file tree
Showing 7 changed files with 251 additions and 467 deletions.
69 changes: 27 additions & 42 deletions ast/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,7 @@ var DefaultBuiltins = [...]*Builtin{
GraphQLSchemaIsValid,

// JSON Schema
JSONSchemaIsValid,
JSONSchemaVerify,
JSONIsMatchSchema,
JSONMatchSchema,

// Cloud Provider Helpers
Expand Down Expand Up @@ -2663,43 +2661,23 @@ var GraphQLSchemaIsValid = &Builtin{
* JSON Schema
*/

// JSONSchemaIsValid returns true if the input is valid JSON schema,
// and returns false for all other inputs.
var JSONSchemaIsValid = &Builtin{
Name: "jsonschema.is_valid",
Description: "Checks that the input is a valid JSON schema object. The schema can be either a JSON string or an JSON object.",
Decl: types.NewFunction(
types.Args(
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
),
types.Named("output", types.B).Description("`true` if the schema is a valid JSON schema. `false` otherwise."),
),
}

// JSONSchemaVerify returns empty string if the input is valid JSON schema
// and returns error string for all other inputs.
var JSONSchemaVerify = &Builtin{
Name: "jsonschema.verify",
Description: "Checks that the input is a valid JSON schema object. The schema can be either a JSON string or an JSON object.",
Decl: types.NewFunction(
types.Args(
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))).
Description("the schema to verify"),
),
types.Named("output", types.S).Description("empty string if the schema is a valid JSON schema. Error string otherwise."),
),
}

// JSONIsMatchSchema returns true if the document matches the JSON schema, otherwise false.
var JSONIsMatchSchema = &Builtin{
Name: "json.is_match_schema",
Description: "Checks that the document matches the JSON schema.",
Decl: types.NewFunction(
types.Args(
types.Named("document", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
),
types.Named("output", types.B).Description("`true` if the document matches the JSON schema. Otherwise `false`."),
types.Named("output", types.NewArray([]types.Type{
types.B,
types.NewAny(types.S, types.Null{}),
}, nil)).
Description("`output` is of the form `[valid, error]`. If the schema is valid, then `valid` is `true`, and `error` is `null`. Otherwise, `valid` is `false` and `error` is a string describing the error."),
),
Categories: objectCat,
}

// JSONMatchSchema returns empty array if the document matches the JSON schema,
Expand All @@ -2709,21 +2687,28 @@ var JSONMatchSchema = &Builtin{
Description: "Checks that the document matches the JSON schema.",
Decl: types.NewFunction(
types.Args(
types.Named("document", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))),
types.Named("document", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))).
Description("document to verify by schema"),
types.Named("schema", types.NewAny(types.S, types.NewObject(nil, types.NewDynamicProperty(types.A, types.A)))).
Description("schema to verify document by"),
),
types.Named("output", types.NewArray(
nil, types.NewObject(
[]*types.StaticProperty{
{Key: "error", Value: types.S},
{Key: "type", Value: types.S},
{Key: "field", Value: types.S},
{Key: "desc", Value: types.S},
},
nil,
types.Named("output", types.NewArray([]types.Type{
types.B,
types.NewArray(
nil, types.NewObject(
[]*types.StaticProperty{
{Key: "error", Value: types.S},
{Key: "type", Value: types.S},
{Key: "field", Value: types.S},
{Key: "desc", Value: types.S},
},
nil,
),
),
)).Description("empty array if the document matches the JSON schema. Array of error objects otherwise."),
}, nil)).
Description("`output` is of the form `[match, errors]`. If the document is valid given the schema, then `match` is `true`, and `errors` is an empty array. Otherwise, `match` is `true` and `errors` is an array of objects describing the error(s)."),
),
Categories: objectCat,
}

/**
Expand Down
63 changes: 9 additions & 54 deletions builtin_metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,6 @@
"internal.member_3",
"internal.print"
],
"json": [
"json.is_match_schema",
"json.match_schema"
],
"jsonschema": [
"jsonschema.is_valid",
"jsonschema.verify"
],
"net": [
"net.cidr_contains",
"net.cidr_contains_matches",
Expand All @@ -123,8 +115,10 @@
],
"object": [
"json.filter",
"json.match_schema",
"json.patch",
"json.remove",
"jsonschema.verify",
"object.filter",
"object.get",
"object.keys",
Expand Down Expand Up @@ -8284,29 +8278,6 @@
},
"wasm": true
},
"json.is_match_schema": {
"args": [
{
"name": "document",
"type": "any\u003cstring, object[any: any]\u003e"
},
{
"name": "schema",
"type": "any\u003cstring, object[any: any]\u003e"
}
],
"available": [
"edge"
],
"description": "Checks that the document matches the JSON schema.",
"introduced": "edge",
"result": {
"description": "`true` if the document matches the JSON schema. Otherwise `false`.",
"name": "output",
"type": "boolean"
},
"wasm": false
},
"json.is_valid": {
"args": [
{
Expand Down Expand Up @@ -8486,10 +8457,12 @@
"json.match_schema": {
"args": [
{
"description": "document to verify by schema",
"name": "document",
"type": "any\u003cstring, object[any: any]\u003e"
},
{
"description": "schema to verify document by",
"name": "schema",
"type": "any\u003cstring, object[any: any]\u003e"
}
Expand All @@ -8500,9 +8473,9 @@
"description": "Checks that the document matches the JSON schema.",
"introduced": "edge",
"result": {
"description": "empty array if the document matches the JSON schema. Array of error objects otherwise.",
"description": "`output` is of the form `[match, errors]`. If the document is valid given the schema, then `match` is `true`, and `errors` is an empty array. Otherwise, `match` is `true` and `errors` is an array of objects describing the error(s).",
"name": "output",
"type": "array[object\u003cdesc: string, error: string, field: string, type: string\u003e]"
"type": "array\u003cboolean, array[object\u003cdesc: string, error: string, field: string, type: string\u003e]\u003e"
},
"wasm": false
},
Expand Down Expand Up @@ -8780,28 +8753,10 @@
},
"wasm": true
},
"jsonschema.is_valid": {
"args": [
{
"name": "schema",
"type": "any\u003cstring, object[any: any]\u003e"
}
],
"available": [
"edge"
],
"description": "Checks that the input is a valid JSON schema object. The schema can be either a JSON string or an JSON object.",
"introduced": "edge",
"result": {
"description": "`true` if the schema is a valid JSON schema. `false` otherwise.",
"name": "output",
"type": "boolean"
},
"wasm": false
},
"jsonschema.verify": {
"args": [
{
"description": "the schema to verify",
"name": "schema",
"type": "any\u003cstring, object[any: any]\u003e"
}
Expand All @@ -8812,9 +8767,9 @@
"description": "Checks that the input is a valid JSON schema object. The schema can be either a JSON string or an JSON object.",
"introduced": "edge",
"result": {
"description": "empty string if the schema is a valid JSON schema. Error string otherwise.",
"description": "`output` is of the form `[valid, error]`. If the schema is valid, then `valid` is `true`, and `error` is `null`. Otherwise, `valid` is `false` and `error` is a string describing the error.",
"name": "output",
"type": "string"
"type": "array\u003cboolean, any\u003cnull, string\u003e\u003e"
},
"wasm": false
},
Expand Down
Loading

0 comments on commit 0501498

Please sign in to comment.