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.

Starter code
or press Ctrl EnterNot run yet

type check

Not run yetparse.ts

Expected result: no type errors.

cases

Not run yetA valid booking

Expected output

1Atlas 4
See 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

1error: A booking must be an object
2error: A booking must be an object
3error: A booking must be an object
See 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

1error: seats must be a number
2error: room must be text
3error: room must be text
See 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" });