Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

139 lines
3.6KB

  1. import re
  2. from cerberus import Validator
  3. # pattern = re.compile('^[a-zA-Z0-9]{1,36}$') # 用于匹配至少一个数字
  4. # m = pattern.match('111aaa3213213123123213123a222')
  5. # print(m)
  6. #
  7. # schema = {
  8. # 'name': {'type': 'string', 'required': True},
  9. # 'age': {'type': 'integer', 'required': True, 'min': 18},
  10. # 'email': {'type': 'string', 'required': True, 'regex': r'\w+@\w+\.\w+'}
  11. # }
  12. # v = Validator(schema)
  13. # print(v.validate({ 'name': '11', 'age': 20, 'email': '764784960@qq.com'}))
  14. # aa = str({ 'name': '11', 'age': 20, 'email': '764784960@qq.com'})
  15. # print(isinstance(aa, dict))
  16. # schema = {'name': {'type': 'string'}}
  17. # v = Validator(schema)
  18. # document = {'name1': 'john doe'}
  19. # print(v.validate(document))
  20. # print(v.validate(document, schema))
  21. schema = {
  22. 'request_id': {
  23. 'type': 'string',
  24. 'required': True,
  25. 'empty': False,
  26. 'regex': r'^[a-zA-Z0-9]{1,36}$'
  27. },
  28. 'command': {
  29. 'type': 'string',
  30. 'required': True,
  31. 'allowed': ['start', 'stop']
  32. },
  33. 'pull_url': {
  34. 'type': 'string',
  35. 'required': False,
  36. 'nullable': True,
  37. 'maxlength': 255
  38. },
  39. 'push_url': {
  40. 'type': 'string',
  41. 'required': False,
  42. 'nullable': True,
  43. 'maxlength': 255
  44. },
  45. 'original_url': {
  46. 'type': 'string',
  47. 'required': False,
  48. 'nullable': True,
  49. 'maxlength': 255
  50. },
  51. 'original_type': {
  52. 'type': 'string',
  53. 'required': False,
  54. 'nullable': True,
  55. 'maxlength': 255
  56. },
  57. 'image_urls': {
  58. 'type': 'list',
  59. 'required': False,
  60. 'schema': {
  61. 'type': 'string',
  62. 'empty': False,
  63. 'maxlength': 5000
  64. }
  65. },
  66. 'results_base_dir': {
  67. 'type': 'string',
  68. 'required': False,
  69. 'nullable': True,
  70. 'regex': r'^[a-zA-Z0-9]{0,36}$'
  71. },
  72. 'models': {
  73. 'type': 'list',
  74. 'required': False,
  75. 'schema': {
  76. 'type': 'dict',
  77. 'required': False,
  78. 'schema': {
  79. 'code': {
  80. 'type': 'string',
  81. 'required': True,
  82. 'empty': False,
  83. 'dependencies': 'categories',
  84. 'regex': r'^[a-zA-Z0-9]{1,255}$'
  85. },
  86. 'categories': {
  87. 'type': 'list',
  88. 'required': True,
  89. 'dependencies': 'code',
  90. 'schema': {
  91. 'type': 'dict',
  92. 'required': True,
  93. 'schema': {
  94. 'id': {
  95. 'type': 'string',
  96. 'required': True,
  97. 'empty': False,
  98. 'regex': r'^[a-zA-Z0-9]{0,255}$'},
  99. 'config': {
  100. 'type': 'dict',
  101. 'required': False,
  102. 'dependencies': 'id',
  103. }
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }
  110. }
  111. v = Validator(schema, allow_unknown=True)
  112. aa={
  113. 'request_id': "111",
  114. 'command': 'start',
  115. 'pull_url': None,
  116. 'push_url': None,
  117. 'original_url': '',
  118. 'original_type': '',
  119. 'image_urls': ['1','1'],
  120. 'results_base_dir': '111',
  121. 'models': [
  122. # {
  123. # 'code': '1',
  124. # 'categories': [
  125. # # {
  126. # # 'id': '1',
  127. # # 'config': {}
  128. # # }
  129. # ]
  130. # }
  131. ]
  132. }
  133. print(v.validate(aa))
  134. print(v.errors)