← All Exercises

Validate Component Props Against a Contract

basicjavascript~20min

Background

Design-system components drift when props are added without a contract. Validating usage explicitly keeps shared components from becoming per-team forks.

Problem

Check required props, declared types and enum values for a component invocation, returning sorted errors that name the offending prop.

Input

JSON object: { contract: { required: string[], types: object, enums?: object }, props: object }

Output

JSON array of "<prop>:<issue>" strings, sorted

Sample tests

Sample 1

Input: {"contract":{"required":["label"],"types":{"label":"string","size":"string"},"enums":{"size":["sm","md"]}},"props":{"size":"xl"}}
Expected: ["label:required","size:not-in-enum"]

Sample 2

Input: {"contract":{"required":["label"],"types":{"label":"string"}},"props":{"label":"Save"}}
Expected: []

Your solution

Learning objective

Enforce component contracts at call sites.