Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ print(result, error)

Data text must contain exactly one complete JSON value; surrounding JSON whitespace (space, tab, carriage return, newline) is allowed.

**Known schema limit:** `validate` / `try_validate` can still accept malformed trailing closing delimiters in schema text (for example `{"type":"integer"}]`). Check schema text with `decode(schema_text)` first, then pass the **original text** to `validate(data, schema_text)`. Schema parsing currently uses [`jsonschema/v5 v5.3.1`](https://github.com/santhosh-tekuri/jsonschema/blob/v5.3.1/resource.go#L269). This limitation does not describe `decode` / `try_decode`.
Schema text must contain exactly one complete JSON value; surrounding JSON whitespace (space, tab, carriage return, newline) is allowed. Malformed schema text returns `invalid schema` and is not cached. On a cache miss, checking the complete schema adds an extra scan whose CPU cost grows with the schema text; cache hits reuse the compiled schema and skip this additional check.

Schemas must be **self-contained**: an external `$ref` (`file://`, `http://`) is rejected (`not allowed`) — this is what keeps the module pure. A bad schema or malformed data text reports `invalid schema` / `invalid data`. Compiled schemas are cached (bounded), so repeated validation against the same schema text avoids recompilation.

Expand Down
148 changes: 148 additions & 0 deletions lib/json/json_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package json_test

import (
"strconv"
"strings"
"sync/atomic"
"testing"

"github.com/1set/starlet/dataconv"
Expand Down Expand Up @@ -1055,6 +1057,103 @@ func TestJSONValidate(t *testing.T) {
assert.eq(e2, None)
`),
},
{
name: `validate: schema trailing closing bracket is rejected`,
script: itn.HereDoc(`
load('json', 'validate')
validate('1', '{"type":"integer","title":"LET34 bracket"}]')
`),
wantErr: `invalid schema`,
},
{
name: `validate: schema trailing closing brace is rejected`,
script: itn.HereDoc(`
load('json', 'validate')
validate('1', '{"type":"integer","title":"LET34 brace"}}')
`),
wantErr: `invalid schema`,
},
{
name: `try_validate: malformed schema tails cannot run`,
script: itn.HereDoc(`
load('json', 'try_validate')
def check():
for schema in ['{}', 'true', 'false']:
for tail in [' null', ']', '}', ' \t\r\n]', '{}', ' false', ' garbage', '/* comment */', '\x0b', '\x0c', '\x00', '\u00a0']:
result, err = try_validate('1', schema + tail)
assert.eq(result, None)
assert.true(err != None and 'invalid schema' in err)
check()
`),
},
{
name: `validate: schema accepts only JSON whitespace around one value`,
script: itn.HereDoc(`
load('json', 'validate', 'try_validate')
def check():
for ws in ['', ' ', '\t', '\r', '\n', ' \t\r\n']:
assert.eq(validate('1', ws + '{"type":"integer"}' + ws), None)
assert.eq(validate('1', ws + 'true' + ws), None)
result, err = try_validate('1', ws + 'false' + ws)
assert.eq(result, False)
assert.true(err != None)
check()
`),
},
{
name: `try_validate: schema bytes have the same strict boundary`,
script: itn.HereDoc(`
load('json', 'try_validate')
ok, err = try_validate('1', b'{"type":"integer"}\n')
assert.eq((ok, err), (True, None))
bad, tail = try_validate('1', b'{"type":"integer"}]')
assert.eq(bad, None)
assert.true(tail != None and 'invalid schema' in tail)
`),
},
{
name: `try_validate: rejected schema cannot become a cached success`,
script: itn.HereDoc(`
load('json', 'try_validate')
def check():
schema = '{"type":"integer","title":"LET34 cache isolation"}'
for data in ['1', '"not an integer"', '2']:
result, err = try_validate(data, schema + ']')
assert.eq(result, None)
assert.true(err != None and 'invalid schema' in err)
assert.eq(try_validate('1', schema), (True, None))
invalid, details = try_validate('"not an integer"', schema)
assert.eq(invalid, False)
assert.true(details != None)
result, err = try_validate('2', schema + ']')
assert.eq(result, None)
assert.true(err != None and 'invalid schema' in err)
check()
`),
},
{
name: `validate: schema and native boolean values remain supported`,
script: itn.HereDoc(`
load('json', 'validate', 'try_validate')
assert.eq(validate(1, {'type': 'integer'}), None)
assert.eq(validate(1, True), None)
result, err = try_validate(1, False)
assert.eq(result, False)
assert.true(err != None)
`),
},
{
name: `try_validate: malformed and non-schema JSON cannot run`,
script: itn.HereDoc(`
load('json', 'try_validate')
def check():
for schema in ['', ' ', '{"type":', 'true false', '{} {}', 'null', '[]', '1']:
result, err = try_validate('1', schema)
assert.eq(result, None)
assert.true(err != None and 'invalid schema' in err)
check()
`),
},
{
name: `validate: draft-7 schema via $schema`,
script: itn.HereDoc(`
Expand Down Expand Up @@ -1261,3 +1360,52 @@ func TestValidateSingleDocument(t *testing.T) {
})
}
}

var jsonSchemaBenchmarkEpoch uint64

// BenchmarkJSONSchemaValidation separates cache hits from compilation of new
// schema texts. Cold cases include constructing each distinct input string.
func BenchmarkJSONSchemaValidation(b *testing.B) {
mod, err := json.LoadModule()
if err != nil {
b.Fatal(err)
}
validate, err := mod[json.ModuleName].(starlark.HasAttrs).Attr("validate")
if err != nil {
b.Fatal(err)
}
for _, tc := range []struct {
name string
cold bool
pad string
}{
{name: "CachedSmall"},
{name: "CachedLarge", pad: strings.Repeat("schema description ", 128)},
{name: "ColdSmall", cold: true},
{name: "ColdLarge", cold: true, pad: strings.Repeat("schema description ", 128)},
} {
b.Run(tc.name, func(b *testing.B) {
// Each calibration uses a fresh namespace, so cold cases never hit
// schema entries left by an earlier benchmark invocation.
epoch := atomic.AddUint64(&jsonSchemaBenchmarkEpoch, 1)
prefix := `{"type":"integer","$comment":"` + tc.pad + strconv.FormatUint(epoch, 10) + ":"
args := starlark.Tuple{starlark.String("1"), starlark.String(prefix + `cached"}`)}
thread := &starlark.Thread{Name: "schema-benchmark"}
if !tc.cold {
if _, err := starlark.Call(thread, validate.(starlark.Callable), args, nil); err != nil {
b.Fatal(err)
}
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
if tc.cold {
args[1] = starlark.String(prefix + strconv.Itoa(i) + `"}`)
}
if _, err := starlark.Call(thread, validate.(starlark.Callable), args, nil); err != nil {
b.Fatal(err)
}
}
})
}
}
15 changes: 10 additions & 5 deletions lib/json/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ var (
// compileSchema compiles (with caching) a JSON Schema from its text. $ref to
// external resources — files or the network — is blocked, keeping the json
// module pure: a schema must be self-contained.
func compileSchema(text string) (*jsonschema.Schema, error) {
func compileSchema(data []byte) (*jsonschema.Schema, error) {
text := string(data)
schemaCacheMu.Lock()
defer schemaCacheMu.Unlock()
if s, ok := schemaCache[text]; ok {
Expand All @@ -43,6 +44,11 @@ func compileSchema(text string) (*jsonschema.Schema, error) {
if err := c.AddResource("inline://schema", strings.NewReader(text)); err != nil {
return nil, err
}
// Preserve resource parsing errors, then reject tails ignored by the
// dependency before the schema can be compiled or cached.
if !json.Valid(data) {
return nil, fmt.Errorf("schema must contain exactly one valid JSON value")
}
s, err := c.Compile("inline://schema")
if err != nil {
return nil, err
Expand Down Expand Up @@ -84,9 +90,8 @@ func formatValidationError(ve *jsonschema.ValidationError) string {
// pointer-per-violation message otherwise. try_validate distinguishes the
// three outcomes: (True, None) valid, (False, details) invalid, and
// (None, error) when validation could not run (bad schema, bad arguments,
// malformed JSON text). Data text must contain exactly one complete JSON value.
// Known limitation: malformed trailing closing delimiters can still be accepted
// in schema text.
// malformed JSON text). Data and schema text must each contain exactly one
// complete JSON value.
func generateValidate(try bool) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
return func(_ *starlark.Thread, fn *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
var data, schema starlark.Value
Expand Down Expand Up @@ -124,7 +129,7 @@ func prepareValidation(data, schema starlark.Value) (*jsonschema.Schema, interfa
if err != nil {
return nil, nil, fmt.Errorf("invalid schema: %w", err)
}
compiled, err := compileSchema(string(schemaBytes))
compiled, err := compileSchema(schemaBytes)
if err != nil {
return nil, nil, fmt.Errorf("invalid schema: %w", err)
}
Expand Down
Loading