Complete the code to return a 422 status when the input data is invalid.
if not valid_data: return {'error': 'Invalid input'}, [1]
The HTTP status code 422 means the server understands the content type and syntax but cannot process the instructions due to semantic errors.
Complete the code to check for missing required fields and respond with 422.
required_fields = ['name', 'email'] if any(field not in data for field in [1]): return {'error': 'Missing fields'}, 422
We check if any required field is missing in the input data by iterating over the list of required fields.
Fix the error in the code to correctly return a 422 response with a JSON error message.
def handle_request(data): if not is_valid(data): return [1]
Using jsonify formats the response as JSON and returns the correct status code.
Fill both blanks to create a dictionary comprehension that filters valid fields and returns 422 if any are missing.
filtered_data = {field: data[field] for field in data if field [1] required_fields}
if len(filtered_data) [2] len(required_fields):
return {'error': 'Missing required fields'}, 422The comprehension keeps fields that are in required_fields. Then it checks if filtered_data has fewer fields than required_fields, meaning some are missing.
Fill all three blanks to create a function that validates input and returns 422 with a message if invalid.
def validate_input(data): errors = {field: 'Required' for field in [1] if field not in [2] if errors: return [3], 422 return None
The function checks which required fields are missing from data, collects errors, and returns them with a 422 status if any are missing.