{"id":4401,"library":"trafaret","title":"Trafaret","description":"Trafaret is a rigid and powerful Python library for validation and parsing data structures. It provides a simple yet expressive way to define data schemas, perform checks, and convert data according to defined rules, offering clear error reporting. It's currently at version 2.1.1 and is actively maintained.","status":"active","version":"2.1.1","language":"en","source_language":"en","source_url":"https://github.com/Deepwalker/trafaret/","tags":["validation","parsing","data-structures","schema-validation"],"install":[{"cmd":"pip install trafaret","lang":"bash","label":"Latest stable version"}],"dependencies":[],"imports":[{"symbol":"trafaret","correct":"import trafaret as t"},{"symbol":"DataError","correct":"from trafaret import DataError"},{"symbol":"construct","correct":"from trafaret.constructor import construct"},{"note":"While t.Key works, explicit import from trafaret.keys is often clearer for specialized key handling.","wrong":"t.Key","symbol":"Key","correct":"from trafaret.keys import Key"}],"quickstart":{"code":"import datetime\nimport trafaret as t\n\n# Define a trafaret for a date dictionary\ndate_validator = t.Dict(\n    year=t.Int,\n    month=t.Int(gte=1, lte=12),\n    day=t.Int(gte=1, lte=31)\n) & (lambda d: datetime.datetime(**d))\n\n# Valid data\nvalid_data = {'year': 2024, 'month': 4, 'day': 12}\ntry:\n    checked_date = date_validator.check(valid_data)\n    print(f\"Valid date: {checked_date}\")\nexcept t.DataError as e:\n    print(f\"Validation failed for valid data: {e.as_dict()}\")\n\n# Invalid data (missing day)\ninvalid_data = {'year': 2024, 'month': 4}\ntry:\n    date_validator.check(invalid_data)\nexcept t.DataError as e:\n    print(f\"Validation failed for invalid data: {e.as_dict()}\")\n\n# Invalid data (incorrect month)\ninvalid_month_data = {'year': 2024, 'month': 13, 'day': 1}\ntry:\n    date_validator.check(invalid_month_data)\nexcept t.DataError as e:\n    print(f\"Validation failed for incorrect month: {e.as_dict()}\")","lang":"python","description":"This quickstart demonstrates how to define a Trafaret for a dictionary representing a date, including integer range validation for month and day. It also shows how to chain a converter function using the `&` operator to transform the validated dictionary into a `datetime.datetime` object. Error handling with `t.DataError` and `as_dict()` is also illustrated."},"warnings":[{"fix":"Replace `t.String(regex='...')` with `t.Regexp('...')` or `t.RegexpRaw('...')`.","message":"The `String` trafaret's `regex` parameter was removed in 2.x. Users should now use `t.Regexp` or `t.RegexpRaw` for regular expression validation.","severity":"breaking","affected_versions":"1.x.x to 2.x.x"},{"fix":"Refactor conversion logic to use the `&` operator, e.g., `t.Int & str` instead of `t.Int(converter=str)`.","message":"The `converters` and `convert=False` arguments for trafarets were removed in 2.x. Custom conversions should now use the `&` operator to chain functions or other trafarets.","severity":"breaking","affected_versions":"1.x.x to 2.x.x"},{"fix":"Assign the result of such operations back to the trafaret variable, e.g., `my_trafaret = my_trafaret.allow_extra('new_key')`.","message":"Trafaret instances are no longer mutable in 2.x. Operations that previously modified a trafaret in place (e.g., `Dict.allow_extra()`, `Dict.make_optional()`) now return a new trafaret instance.","severity":"breaking","affected_versions":"1.x.x to 2.x.x"},{"fix":"Update all instances of `t.StrBool` to `t.ToBool`.","message":"The `StrBool` trafaret was renamed to `ToBool` in 2.x for clarity and consistency with other explicit conversion trafarets like `ToInt` and `ToFloat`.","severity":"deprecated","affected_versions":"1.x.x to 2.x.x"},{"fix":"Be aware that `construct({'age': int})` will now convert '5' to 5. If strict type checking without conversion is desired, use `t.Type(int)` explicitly.","message":"When using `trafaret.constructor.construct` in versions 2.0.2 and later, native Python `int` and `float` types passed as schema values will now automatically use `t.ToInt` and `t.ToFloat` respectively, implying conversion behavior. Earlier versions might have just validated the type.","severity":"gotcha","affected_versions":">=2.0.2"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}