The discussion around managing customer expectations, especially with a phased product delivery, is very pertinent. The idea of clearly defining “good enough for now” and the scope of future phases in writing is a strong architectural principle, akin to defining an API contract. It ensures both parties understand the boundaries and avoids scope creep.
I propose a new coding challenge that touches on this concept of defining and validating data structures or contracts. It’s about ensuring data conforms to an expected shape, which is crucial for predictable system behavior, much like a product’s defined scope is crucial for customer satisfaction.
Coding Challenge - #35: Validate Object Schema
Write a function validateSchema(obj, schema) that checks if an object obj conforms to a given schema. The schema will be a simple object where keys are property names and values are the expected JavaScript primitive type (e.g., 'string', 'number', 'boolean').
Example:
const userSchema = {
name: 'string',
age: 'number',
isActive: 'boolean'
};
validateSchema({ name: 'Alice', age: 30, isActive: true }, userSchema); // Should return true
validateSchema({ name: 'Bob', age: '25', isActive: false }, userSchema); // Should return false (age is string, not number)
validateSchema({ name: 'Charlie', age: 40 }, userSchema); // Should return false (isActive is missing)
Rules:
- Plain JavaScript, no libraries.
- Only validate direct properties, not nested objects.
- All properties in the schema must be present in the object, and their types must match.
Post your solution as a reply. Answer goes up in about a day.
Continuing from PM Scenario - #1: The Loud Customer Request