You are given a simplified protobuf-like schema definition and a JSON value. Determine whether the JSON conforms to the schema.
Schema model
-
A
schema
contains named
message types
.
-
Each message type contains
fields
. Each field has:
-
name
(string)
-
type
(one of:
int
,
float
,
bool
,
string
, or another message type name)
-
optional
(boolean)
Assume:
-
The JSON root is an
object
corresponding to a specified root message type.
-
A
required
field (
optional = false
) must be present.
-
An
optional
field may be missing.
-
If a field is present, its JSON value must match the expected type:
-
int
: JSON number with no fractional part
-
float
: JSON number (integer values are allowed)
-
bool
: JSON boolean
-
string
: JSON string
-
message type: JSON object that recursively validates against that message
-
Extra JSON keys not in the schema make the JSON invalid.
Task
Implement isValid(schema, rootTypeName, jsonValue) -> bool.
Constraints
-
Up to ~10^4 total fields across all message types.
-
Message nesting depth is bounded (e.g., ≤ 50).
Example
Schema:
-
User { id:int (required), name:string (optional) }
Valid JSON: { "id": 1 }
Invalid JSON: { "name": "a" } (missing required id)
Invalid JSON: { "id": 1, "age": 10 } (unknown field age)