schema.py 990 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. from ninja import Schema
  2. def to_camel(string: str, id_string: str) -> str:
  3. """If a word is exactly id, make it Id"""
  4. return "".join(
  5. word if i == 0 else id_string if word == "id" else word.capitalize()
  6. for i, word in enumerate(string.split("_"))
  7. )
  8. def to_camel_upper(string: str) -> str:
  9. return to_camel(string, "ID")
  10. def to_camel_lower(string: str) -> str:
  11. return to_camel(string, "Id")
  12. class CamelSchema(Schema):
  13. """
  14. Use json camel case convention by default
  15. Preferred camel case schema
  16. - event_id > eventID
  17. - event_number > eventNumber
  18. - foobar_100 > foobar100
  19. """
  20. class Config(Schema.Config):
  21. alias_generator = to_camel_upper
  22. populate_by_name = True
  23. class CamelWithLowerIdSchema(Schema):
  24. '''
  25. Use json camel case convention by default
  26. For Sentry compatibility on issues
  27. '''
  28. class Config(Schema.Config):
  29. alias_generator = to_camel_lower
  30. populate_by_name = True