This question evaluates schema validation and data-typing skills, including understanding of JSON primitive types, recursive message structures, and conformance to a protobuf-like schema with required and optional fields.
You are given a simplified protobuf-like schema definition and a JSON value. Determine whether the JSON conforms to the schema.
name
(string)
type
(one of:
int
,
float
,
bool
,
string
, or another message type name)
optional
(boolean)
Assume:
optional = false
) must be present.
int
: JSON number with no fractional part
float
: JSON number (integer values are allowed)
bool
: JSON boolean
string
: JSON string
Implement isValid(schema, rootTypeName, jsonValue) -> bool.
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)