Check the response
EasyErrors7 min
Write a class ValidationError that extends Error and has the name 'ValidationError'. Then write a function readUser(json). It parses a JSON string and returns the user object. If the object has no name or no age field, throw a ValidationError with the message 'Missing field: name' or 'Missing field: age'. If the string is not JSON, the SyntaxError from JSON.parse goes on as it is.
Examples
readUser('{"name":"Ann","age":30}')Returns { name: 'Ann', age: 30 }
new ValidationError('Missing field: age').nameReturns 'ValidationError'
Loading the editor...
- Input
readUser('{"name":"Ann","age":30}')Expected{ name: 'Ann', age: 30 } - Input
readUser('{"name":"Ann"}')ExpectedThrows ValidationError 'Missing field: age' - The thrown error is a ValidationError and an Error
Hidden tests run on Submit