schema.py 537 B

1234567891011121314151617181920212223
  1. from ninja import Schema
  2. def to_camel(string: str) -> str:
  3. """If a word is exactly id, make it ID"""
  4. return "".join(
  5. word if i == 0 else "ID" if word == "id" else word.capitalize()
  6. for i, word in enumerate(string.split("_"))
  7. )
  8. class CamelSchema(Schema):
  9. """
  10. Use json camel case convention by default
  11. - event_id > eventID
  12. - event_number > eventNumber
  13. - foobar_100 > foobar100
  14. """
  15. class Config(Schema.Config):
  16. alias_generator = to_camel
  17. populate_by_name = True