Lab 2 of 5
Β·
Open-Ended
Write validator.py β Validation Logic
Objective: Write a custom exception and a validate_device() function that checks required fields, IP format, and VLAN range.
Instructions
Inside capstone/, write validator.py from scratch.
It must define:
InvalidDeviceRecord(Exception)β a custom exception classREQUIRED_FIELDSβ a list of the five fields every record must havevalidate_device(record)β raisesInvalidDeviceRecordwith a specific message for each failure:- If a required field is missing:
Missing field: <field> - If the IP does not look like four dot-separated groups of digits:
Invalid IP: <ip> - If the VLAN is not between 1 and 4094:
VLAN out of range: <vlan> - Returns
Trueif all checks pass
Check fields in the order listed above β missing fields first, then IP, then VLAN.
Do not copy from the lesson. You wrote a custom exception in Unit 8 and used regex in Unit 10.
Paste your completed validator.py below and submit.
π‘ Show Hint
class InvalidDeviceRecord(Exception): pass. Loop through REQUIRED_FIELDS and raise if field not in record. Use re.match(r"^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$", record["ip"]) for the IP check. int(record["vlan"]) to check the range.
Submit Your Code