Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pullrequest-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ jobs:
install: true
install_args: python poetry
cache: true
version: 2026.9.2
- name: Install Python dependencies
uses: ZeroGachis/.github/.github/actions/poetry-install@v6
with:
Expand Down
3 changes: 3 additions & 0 deletions magicparse/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ def __init__(self, key: str, options: dict[str, Any]) -> None:
post_processors = [PostProcessor.build(item) for item in options.get("post-processors", [])]

self.optional = options.get("optional", False)
self.on_type_error = type_converter.on_error

self.transforms = pre_processors + [type_converter] + validators + post_processors

def _process_raw_value(self, raw_value: str) -> Result:
if not raw_value:
if self.optional:
return Ok(value=None)
elif self.on_type_error == OnError.SKIP_ROW:
return SkipRow(ValueError(f"{self.key} field is required but the value was empty"))
else:
raise ValueError(f"{self.key} field is required but the value was empty")
for transform in self.transforms:
Expand Down
18 changes: 16 additions & 2 deletions tests/test_fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
from typing import Any

import pytest
from magicparse.transform import Ok
from magicparse.type_converters import DecimalConverter, StrConverter

from magicparse.fields import ColumnarField, CsvField, Field
from magicparse.post_processors import Divide
from magicparse.pre_processors import Replace, StripWhitespaces
from magicparse.transform import Ok, SkipRow
from magicparse.type_converters import DecimalConverter, StrConverter
from magicparse.validators import RegexMatches


Expand Down Expand Up @@ -127,6 +128,19 @@ def test_require_field_with_empty_value():
field.parse("")


def test_ignore_require_field_with_empty_value():
field = DummyField(
"pepito",
{
"type": {"key": "decimal", "on-error": "skip-row"},
},
)
result = field.parse("")
assert isinstance(result, SkipRow)
assert type(result.exception) is ValueError
assert str(result.exception) == "pepito field is required but the value was empty"


def test_field_without_key():
with pytest.raises(ValueError, match="key is required in field definition"):
Field.build({"type": "decimal"})
Expand Down
Loading