One of the most important goals of the data platform is to ensure data quality. One of the mechanisms is using JSON schema, as discussed above. However, we also provide ability to provide custom rules for your dataset. Here are some scenarios when it may be useful.
Contextual validation. While JSON schema works for most scenarios when we need to validate specific value (for example, number of rooms in apartment), it is impossible to validate context (for example that when room number is 1, area should be less than 50 square meters).
Custom logics. You may need very custom logic, for example normalize value (make string lowercase and then check if its one of the list).
We support warning and error level validators. Warning level validators indicate that data MAY be invalid and its implemented by printing into console (which is shown error message). Error messages are pipeline blocking and needs immediate attention. They implement by throwing exception with error message. Input data is read from standard input as JSON.
Example validator
Lets explore real life scenario – custom validator to ensure that each apartment in the list has reasonable square meter price. For this we define (larger) range where it would throw exception if outside, and narrower range for warnings.
import sys
import json
apartments = json.loads(sys.stdin.read())
# CONFIG START
WARNING_PRICE_MIN = 1500
WARNING_PRICE_MAX = 6000
ERROR_PRICE_MIN = 500
ERROR_PRICE_MAX = 10000
# CONFIG END
has_price = False
for apartment in apartments:
flat_id = apartment.get('id')
price = apartment.get('price')
area = apartment.get('area')
price_sqm = None
if area and price:
has_price = True
price_sqm = price / area
if price_sqm < ERROR_PRICE_MIN:
raise Exception('Price for %s too low %f' % (flat_id, price_sqm))
elif price_sqm > ERROR_PRICE_MAX:
raise Exception('Price for %s too high %f' % (flat_id, price_sqm))
elif price_sqm < WARNING_PRICE_MIN:
print('Price for %s MAY BE too low %f' % (flat_id, price_sqm))
elif price_sqm > WARNING_PRICE_MAX:
print('Price for %s MAY BE too high %f' % (flat_id, price_sqm))
Lets break down parts of validator. First lets, lets read the data:
import sys
import json
apartments = json.loads(sys.stdin.read())
Then write your logic. This is example of warning level handling:
elif price_sqm < WARNING_PRICE_MIN:
print('Price for %s MAY BE too low %f' % (flat_id, price_sqm
This is example of error level handling:
elif price_sqm > ERROR_PRICE_MAX:
raise Exception('Price for %s too high %f' % (flat_id, price_sqm))
It is possible scenario that validator triggers both warning and error. In that case higher importance (error) level validation is shown in our tool.
Testing your validator
Now you have your validator code its time to test it! We recommend to test with valid and invalid scenario to make sure it works. First lets create valid JSON file APARTMENTS_VALID.json:
[{‘area’: 50, ‘price’: 200000, ‘id’: ‘1’}]
Test it by piping via command line using terminal:
>>
>> python3 price_validator.py < APARTMENTS_VALID.json
>>
Since data in JSON is valid, as expected nothing will be printed. Now lets created invalid data sample APARTMENTS_INVALID.JSON
[{‘area’: 50, ‘price’: 20000000, ‘id’: ‘1’}]
Lets run it in command line:
>>
>> python3 price_validator.py < APARTMENTS_INVALID.json
>> Price for 1 too high 200000000!
We have just completed writing and testing our first validator! Good luck creating new rules to ensure data quality.
Best practises
Better don’t mix several ideas into one code – for example check average price as above, and also check whether apartment ID’s are unique, etc. Better write separate code, since in case of error (Exception thrown) code execution will stop, while there be more insights about code quality.