Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. DATA = {
  2. 'name' : 'Christian',
  3. 'address' : {
  4. 'street' : "Straße",
  5. 'city' : "Aachen",
  6. 'zip' : 12345
  7. },
  8. 'more' : [
  9. {
  10. 'street' : "Straße 1",
  11. 'city' : "Aachen 1",
  12. 'zip' : 1
  13. },
  14. {
  15. 'street' : "Straße 2",
  16. 'city' : "Aachen 2",
  17. 'zip' : 2
  18. },
  19. {
  20. 'street' : "Straße 3",
  21. 'city' : "Aachen 3",
  22. 'zip' : 3
  23. },
  24. ],
  25. 'additional' : "foobar",
  26. }
  27. ERROR = {
  28. 'address' : {
  29. 'street' : "Straße",
  30. 'city' : "Aachen",
  31. 'zip' : 12345
  32. },
  33. 'more' : [
  34. {
  35. 'street' : "Straße 1",
  36. 'city' : "Aachen 1",
  37. 'zip' : 1
  38. },
  39. {
  40. 'street' : "Straße 2",
  41. 'city' : "Aachen 2",
  42. 'zip' : 2
  43. },
  44. {
  45. 'street' : "Straße 3",
  46. 'city' : "Aachen 3",
  47. 'zip' : 3
  48. },
  49. ],
  50. 'additional' : "foobar",
  51. }
  52.  
  53. from cerberus import Validator
  54. address_schema = {
  55. 'street': {'type': 'string', 'required': True, 'minlength': 2},
  56. 'city': {'type': 'string', 'required': True, 'minlength': 2},
  57. 'zip': {'type': 'integer', 'required': True, 'max': 99999}
  58. }
  59. schema = {
  60. 'name': {'type': 'string', 'required': True, 'minlength': 2},
  61. 'address': {
  62. 'type': 'dict',
  63. 'schema': address_schema,
  64. },
  65. 'hello' : {'type' : 'string', 'default': 'nose'},
  66. 'more' : {
  67. 'type' : 'list',
  68. 'schema' : {
  69. 'type' : 'dict',
  70. 'schema': address_schema,
  71. },
  72. }
  73. }
  74.  
  75. v = Validator(schema, purge_unknown=True)
  76. print(v.validate(DATA))
  77. import pprint
  78. pprint.pprint(v.document)
  79.  
  80. print(v.validate(ERROR))
  81. print(v.errors)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement