Check the booking before you use it
Validate incoming booking data before reading its fields.
your task
Keep the input and return types. Validate without using as. Reject nonobjects, null, and arrays with “A booking must be an object”. Check room before seats. For missing or invalid fields, throw “room must be text” or “seats must be a number”. Return the valid room and seat count.
The type check passes, but parseBooking accepts invalid response data. A booking must be an object with a text room and a numeric seat count. The caller needs a specific error for each invalid input.
Run code does two things. First the compiler checks the types and lists each error with its line. Then your code runs once for each case. A type error does not stop the run. The exercise is solved when there are no type errors and every case passes.
type check
Not run yetparse.ts
Expected result: no type errors.
cases
Not run yetA valid booking
Expected output
Atlas 4See the input and the code for this case
function show(value) {
try {
const booking = parseBooking(value);
console.log(booking.room + " " + booking.seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show({ room: "Atlas", seats: 4 });Not run yetNull, text, or an array
Expected output
error: A booking must be an objecterror: A booking must be an objecterror: A booking must be an objectSee the input and the code for this case
function show(value) {
try {
const booking = parseBooking(value);
console.log(booking.room + " " + booking.seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show(null);
show("Atlas");
show([{ room: "Atlas", seats: 4 }]);Not run yetMissing or invalid booking fields
Expected output
error: seats must be a numbererror: room must be texterror: room must be textSee the input and the code for this case
function show(value) {
try {
const booking = parseBooking(value);
console.log(booking.room + " " + booking.seats);
} catch (error) {
console.log("error: " + error.message);
}
}
show({ room: "Atlas", seats: "4" });
show({ seats: 4 });
show({ room: 7, seats: "4" });