NixRam
NixRam

Reputation: 519

Cant validate a list of values for duplicates using Python and Cerberus

I am fairly new to Python and Cerberus. I have a requirement where I need to validate a list for any empty Strings or duplicates. Below is what I did:

import cerberus

myschema = {'uid': {'type': 'list', 'schema': {'type': 'string', 'required' : True}}}

cerberus.rules_set_registry.add('myschema', myschema)
newval = Validator(myschema)

test = {'uid' : {'10000', '10001', '10002', '10003', '10004', '10005'}}
newval.validate(test)

The output is always 'False' for some reason.
Alternatively, I tried the 'oneof' of-rules and came up with the below:

from cerberus import Validator
document = {'column_name' : ['BLAH', 'SEX', 'DOMAIN', 'DOMAIN']}

schema = {'column_name' : {'oneof' : [{'type': 'list', 'contains' : ['DOMAIN']}]} }
v = Validator(schema)
v.validate(document, schema)

The above always return True. I was hoping the 'oneof' can validate duplicates and the aforementioned is the right way. Can anyone here please correct me if I am wrong.!
Thanks in advance,
Nix

Upvotes: 0

Views: 1164

Answers (2)

vishnukanth
vishnukanth

Reputation: 31

test = {'uid' : {'10000', '10001', '10002', '10003', '10004', '10005'}}

This is not a valid representation of list in json. The following is

test = {'uid' : ['10000', '10001', '10002', '10003', '10004', '10005']}

That is why Cerberus returns false. You could use print(newval.errors) to print all the errors for that json object.

As for oneOf, Cerberus will validate that your items in the list will meet exactly one of the constraints mentioned in the constraint list. It doesn't check the relationship between the elements.

Upvotes: 2

NixRam
NixRam

Reputation: 519

Alright.
I couldn't find an answer with Cerberus validator to check duplicates in a list.
I checked Json Schema where it is really easy. Below is the code:

from jsonschema import validate

schema = { "type": "array", "uniqueItems": True}

mylist  = ['1000', '1001', '1010', '1011', '1100', '1101', '1110', '1000']

# Returns None if validated; else a validation error
validate(instance = mylist, schema = schema)

In Cerberus, validation returns a True if validated right; else False. Whereas, Json Schema returns a None (in Python) if validated right else, throws a validation error as given below:

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-1-efa3c7f0da39> in <module>
      5 mylist  = ['1000', '1001', '1010', '1011', '1100', '1101', '1110', '1000']
      6 
----> 7 ans = validate(instance = mylist, schema = schema)
      8 if(ans == None): print('Validated as True')

~/Library/Python/3.8/lib/python/site-packages/jsonschema/validators.py in validate(instance, schema, cls, *args, **kwargs)
    932     error = exceptions.best_match(validator.iter_errors(instance))
    933     if error is not None:
--> 934         raise error
    935 
    936 

ValidationError: ['1000', '1001', '1010', '1011', '1100', '1101', '1110', '1000'] has non-unique elements

Failed validating 'uniqueItems' in schema:
    {'type': 'array', 'uniqueItems': True}

On instance:
    ['1000', '1001', '1010', '1011', '1100', '1101', '1110', '1000']

Upvotes: 0

Related Questions