Skip to content

fix: support typed Go slices and maps in validation - #255

Open
Yanhu007 wants to merge 1 commit into
santhosh-tekuri:boonfrom
Yanhu007:fix/typed-slice-validation
Open

fix: support typed Go slices and maps in validation#255
Yanhu007 wants to merge 1 commit into
santhosh-tekuri:boonfrom
Yanhu007:fix/typed-slice-validation

Conversation

@Yanhu007

Copy link
Copy Markdown

Fixes #238

Problem

The validator only accepts []any for JSON arrays and map[string]any for JSON objects. Typed Go slices like []string or []map[string]interface{} are rejected with:

invalid jsonType []string

Even though these types serialize correctly to JSON and match the schema.

Fix

Use reflect as a fallback in typeOf() to detect:

  • Slices/arrays → treated as JSON arrays
  • Maps with string keys → treated as JSON objects

When validation needs to iterate over these values (in arrValidate/objValidate and unevalFrom), the typed collections are converted to []any / map[string]any first.

Testing

All existing tests pass. Manually verified:

schema.Validate([]string{"hello", "world"})          // ✅ was: invalid jsonType
schema.Validate([]map[string]string{{"key": "val"}}) // ✅ was: invalid jsonType

Previously, the validator only accepted []any for JSON arrays and
map[string]any for JSON objects. Go values like []string or
[]map[string]interface{} were rejected with 'invalid jsonType'
errors, even though they represent valid JSON arrays and objects.

Use reflect as a fallback in typeOf() to detect slices, arrays,
and string-keyed maps. Convert them to []any / map[string]any
before passing to arrValidate / objValidate.

Fixes santhosh-tekuri#238
@santhosh-tekuri
santhosh-tekuri force-pushed the boon branch 3 times, most recently from 7ba9b5e to b0fc661 Compare June 28, 2026 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validation fails for []string and []map[string]interface{} unless explicitly cast to []any

1 participant