Skip to content

Encode AT string field values to native str before validation - #101

Open
ramonski wants to merge 2 commits into
feature/normalize-dx-uid-referencesfrom
feature/encode-at-string-values
Open

Encode AT string field values to native str before validation#101
ramonski wants to merge 2 commits into
feature/normalize-dx-uid-referencesfrom
feature/encode-at-string-values

Conversation

@ramonski

Copy link
Copy Markdown
Contributor

What

ATFieldManager._set now utf-8-encodes unicode text values before the field validates them.

Why

AT field validators (isEmail, isDecimal, ...) require a native str. Values from a JSON request body arrive as unicode, so they failed:

POST .../Client/update  {"EmailAddress": "info@example.com"}
-> Invalid value for field EmailAddress: Validation failed(isEmail):
   info@example.com of type <type 'unicode'>, expected 'string'

POST .../AnalysisService/update  {"Price": "12.50"}
-> Validation failed(isDecimal): 12.50 of type <type 'unicode'>, expected 'string'

So EmailAddress, Price, VAT, DuplicateVariation (and any AT field with a strict string validator) could not be set via the API. This mirrors the utf-8 handling bika.lims.api.validate already does on the read path.

Change

Before self.field.validate(...), encode text values with bika.lims.api.to_utf8, guarded by isinstance(value, six.text_type) so dicts/lists/numbers pass through untouched — records/datagrid/reference fields are unaffected. Native str (e.g. form submissions) is also untouched.

Testing

New create doctest posts a JSON body (Price: u"12.50") and asserts the service is created with getPrice() == '12.50'. Full suite green (31/0). The existing form-encoded doctests (native str) confirm no regression.

Stacking

On top of #100.

ramonski added 2 commits July 27, 2026 13:50
AT field validators (isEmail, isDecimal, ...) expect a native str, but
values from a JSON body arrive as unicode and fail with "expected
'string'" -- so EmailAddress, Price, VAT, DuplicateVariation could not be
set through the JSON API. ATFieldManager._set now utf-8-encodes text
values before validation; dicts/lists/other types are left untouched, so
records/datagrid/reference fields are unaffected. Adds a create doctest
that posts a JSON body with a unicode decimal.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Bug 🐞 Something is broken

Development

Successfully merging this pull request may close these issues.

1 participant