Validate User Record (Multi Errors)
JavaScript
Medium
3 views
Problem Description
One JSON object is given with fields name, age, email. Check: name is non-empty string, age is integer 1..120, email contains @. Print OK if valid else print all errors joined by ;
Input Format
One line JSON object.
Output Format
OK or error list.
Sample Test Case
Input:
{"name":"","age":200,"email":"abc"}
Constraints
Input is valid JSON.
Official Solution
const fs=require('fs');const s=fs.readFileSync(0,'utf8').trim();if(!s)process.exit(0);try{const o=JSON.parse(s);let errs=[];if(typeof o.name!=='string'||!o.name.trim())errs.push('name');if(!Number.isInteger(o.age)||o.age<1||o.age>120)errs.push('age');if(typeof o.email!=='string'||!o.email.includes('@'))errs.push('email');if(errs.length)throw new Error(errs.join(';'));process.stdout.write('OK');}catch(e){process.stdout.write(e.message||'BAD');}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!