dhi 1.1.1__cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dhi/__init__.py +173 -0
- dhi/_dhi_native.cpython-313-aarch64-linux-gnu.so +0 -0
- dhi/_native.c +379 -0
- dhi/batch.py +236 -0
- dhi/constraints.py +358 -0
- dhi/datetime_types.py +108 -0
- dhi/fields.py +187 -0
- dhi/functional_validators.py +108 -0
- dhi/libsatya.so +0 -0
- dhi/model.py +658 -0
- dhi/networks.py +290 -0
- dhi/secret.py +105 -0
- dhi/special_types.py +359 -0
- dhi/types.py +345 -0
- dhi/validator.py +212 -0
- dhi-1.1.1.dist-info/METADATA +115 -0
- dhi-1.1.1.dist-info/RECORD +21 -0
- dhi-1.1.1.dist-info/WHEEL +6 -0
- dhi-1.1.1.dist-info/licenses/LICENSE +21 -0
- dhi-1.1.1.dist-info/top_level.txt +1 -0
- dhi.libs/libsatya-a22d98f4.so +0 -0
dhi/__init__.py
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
"""
|
|
2
|
+
dhi - High-performance data validation for Python, powered by Zig
|
|
3
|
+
|
|
4
|
+
A Python validation library with Pydantic v2 compatible API and
|
|
5
|
+
blazing-fast native performance via Zig/C extensions.
|
|
6
|
+
|
|
7
|
+
Example:
|
|
8
|
+
from typing import Annotated
|
|
9
|
+
from dhi import BaseModel, Field, PositiveInt, EmailStr
|
|
10
|
+
|
|
11
|
+
class User(BaseModel):
|
|
12
|
+
name: Annotated[str, Field(min_length=1, max_length=100)]
|
|
13
|
+
age: PositiveInt
|
|
14
|
+
email: EmailStr
|
|
15
|
+
score: Annotated[float, Field(ge=0, le=100)] = 0.0
|
|
16
|
+
|
|
17
|
+
user = User(name="Alice", age=25, email="alice@example.com")
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
__version__ = "0.2.0"
|
|
21
|
+
__author__ = "Rach Pradhan"
|
|
22
|
+
|
|
23
|
+
# --- Core validators (original API) ---
|
|
24
|
+
from .validator import (
|
|
25
|
+
BoundedInt,
|
|
26
|
+
BoundedString,
|
|
27
|
+
Email,
|
|
28
|
+
ValidationError,
|
|
29
|
+
ValidationErrors,
|
|
30
|
+
HAS_NATIVE_EXT,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
# --- Batch API ---
|
|
34
|
+
from .batch import (
|
|
35
|
+
BatchValidationResult,
|
|
36
|
+
validate_users_batch,
|
|
37
|
+
validate_ints_batch,
|
|
38
|
+
validate_strings_batch,
|
|
39
|
+
validate_emails_batch,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# --- Constraints (Pydantic v2 compatible) ---
|
|
43
|
+
from .constraints import (
|
|
44
|
+
Gt, Ge, Lt, Le, MultipleOf,
|
|
45
|
+
MinLength, MaxLength, Pattern,
|
|
46
|
+
Strict, StripWhitespace, ToLower, ToUpper,
|
|
47
|
+
AllowInfNan, MaxDigits, DecimalPlaces, UniqueItems,
|
|
48
|
+
StringConstraints,
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# --- Field ---
|
|
52
|
+
from .fields import Field, FieldInfo
|
|
53
|
+
|
|
54
|
+
# --- Type aliases (Pydantic v2 compatible) ---
|
|
55
|
+
from .types import (
|
|
56
|
+
# Strict types
|
|
57
|
+
StrictInt, StrictFloat, StrictStr, StrictBool, StrictBytes,
|
|
58
|
+
# Positive/Negative integers
|
|
59
|
+
PositiveInt, NegativeInt, NonNegativeInt, NonPositiveInt,
|
|
60
|
+
# Positive/Negative floats
|
|
61
|
+
PositiveFloat, NegativeFloat, NonNegativeFloat, NonPositiveFloat,
|
|
62
|
+
FiniteFloat,
|
|
63
|
+
# con* functions
|
|
64
|
+
conint, confloat, constr, conbytes,
|
|
65
|
+
conlist, conset, confrozenset,
|
|
66
|
+
condecimal, condate,
|
|
67
|
+
)
|
|
68
|
+
|
|
69
|
+
# --- BaseModel ---
|
|
70
|
+
from .model import BaseModel
|
|
71
|
+
|
|
72
|
+
# --- Network types ---
|
|
73
|
+
from .networks import (
|
|
74
|
+
EmailStr, NameEmail,
|
|
75
|
+
AnyUrl, AnyHttpUrl, HttpUrl, FileUrl, FtpUrl,
|
|
76
|
+
WebsocketUrl, AnyWebsocketUrl,
|
|
77
|
+
PostgresDsn, CockroachDsn, MySQLDsn, MariaDBDsn,
|
|
78
|
+
ClickHouseDsn, MongoDsn, RedisDsn, AmqpDsn,
|
|
79
|
+
KafkaDsn, NatsDsn, SnowflakeDsn,
|
|
80
|
+
IPvAnyAddress, IPvAnyInterface, IPvAnyNetwork,
|
|
81
|
+
)
|
|
82
|
+
|
|
83
|
+
# --- Date/Time types ---
|
|
84
|
+
from .datetime_types import (
|
|
85
|
+
PastDate, FutureDate,
|
|
86
|
+
PastDatetime, FutureDatetime,
|
|
87
|
+
AwareDatetime, NaiveDatetime,
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
# --- Functional validators ---
|
|
91
|
+
from .functional_validators import (
|
|
92
|
+
field_validator, model_validator, validator,
|
|
93
|
+
)
|
|
94
|
+
|
|
95
|
+
# --- Secret types ---
|
|
96
|
+
from .secret import SecretStr, SecretBytes
|
|
97
|
+
|
|
98
|
+
# --- Special types ---
|
|
99
|
+
from .special_types import (
|
|
100
|
+
UUID1, UUID3, UUID4, UUID5,
|
|
101
|
+
FilePath, DirectoryPath, NewPath,
|
|
102
|
+
Base64Bytes, Base64Str, Base64UrlBytes, Base64UrlStr,
|
|
103
|
+
Json, ImportString, ByteSize,
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
# Try to import native extension
|
|
107
|
+
try:
|
|
108
|
+
from . import _dhi_native
|
|
109
|
+
except ImportError:
|
|
110
|
+
_dhi_native = None
|
|
111
|
+
|
|
112
|
+
|
|
113
|
+
__all__ = [
|
|
114
|
+
# Core validators (original API)
|
|
115
|
+
"BoundedInt", "BoundedString", "Email",
|
|
116
|
+
"ValidationError", "ValidationErrors",
|
|
117
|
+
"HAS_NATIVE_EXT", "_dhi_native",
|
|
118
|
+
|
|
119
|
+
# Batch validation
|
|
120
|
+
"BatchValidationResult",
|
|
121
|
+
"validate_users_batch", "validate_ints_batch",
|
|
122
|
+
"validate_strings_batch", "validate_emails_batch",
|
|
123
|
+
|
|
124
|
+
# Constraints
|
|
125
|
+
"Gt", "Ge", "Lt", "Le", "MultipleOf",
|
|
126
|
+
"MinLength", "MaxLength", "Pattern",
|
|
127
|
+
"Strict", "StripWhitespace", "ToLower", "ToUpper",
|
|
128
|
+
"AllowInfNan", "MaxDigits", "DecimalPlaces", "UniqueItems",
|
|
129
|
+
"StringConstraints",
|
|
130
|
+
|
|
131
|
+
# Field
|
|
132
|
+
"Field", "FieldInfo",
|
|
133
|
+
|
|
134
|
+
# Type aliases
|
|
135
|
+
"StrictInt", "StrictFloat", "StrictStr", "StrictBool", "StrictBytes",
|
|
136
|
+
"PositiveInt", "NegativeInt", "NonNegativeInt", "NonPositiveInt",
|
|
137
|
+
"PositiveFloat", "NegativeFloat", "NonNegativeFloat", "NonPositiveFloat",
|
|
138
|
+
"FiniteFloat",
|
|
139
|
+
|
|
140
|
+
# con* functions
|
|
141
|
+
"conint", "confloat", "constr", "conbytes",
|
|
142
|
+
"conlist", "conset", "confrozenset",
|
|
143
|
+
"condecimal", "condate",
|
|
144
|
+
|
|
145
|
+
# BaseModel
|
|
146
|
+
"BaseModel",
|
|
147
|
+
|
|
148
|
+
# Network types
|
|
149
|
+
"EmailStr", "NameEmail",
|
|
150
|
+
"AnyUrl", "AnyHttpUrl", "HttpUrl", "FileUrl", "FtpUrl",
|
|
151
|
+
"WebsocketUrl", "AnyWebsocketUrl",
|
|
152
|
+
"PostgresDsn", "CockroachDsn", "MySQLDsn", "MariaDBDsn",
|
|
153
|
+
"ClickHouseDsn", "MongoDsn", "RedisDsn", "AmqpDsn",
|
|
154
|
+
"KafkaDsn", "NatsDsn", "SnowflakeDsn",
|
|
155
|
+
"IPvAnyAddress", "IPvAnyInterface", "IPvAnyNetwork",
|
|
156
|
+
|
|
157
|
+
# Date/Time types
|
|
158
|
+
"PastDate", "FutureDate",
|
|
159
|
+
"PastDatetime", "FutureDatetime",
|
|
160
|
+
"AwareDatetime", "NaiveDatetime",
|
|
161
|
+
|
|
162
|
+
# Functional validators
|
|
163
|
+
"field_validator", "model_validator", "validator",
|
|
164
|
+
|
|
165
|
+
# Secret types
|
|
166
|
+
"SecretStr", "SecretBytes",
|
|
167
|
+
|
|
168
|
+
# Special types
|
|
169
|
+
"UUID1", "UUID3", "UUID4", "UUID5",
|
|
170
|
+
"FilePath", "DirectoryPath", "NewPath",
|
|
171
|
+
"Base64Bytes", "Base64Str", "Base64UrlBytes", "Base64UrlStr",
|
|
172
|
+
"Json", "ImportString", "ByteSize",
|
|
173
|
+
]
|
|
Binary file
|
dhi/_native.c
ADDED
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Native CPython extension for dhi
|
|
3
|
+
* Links against libsatya.dylib (Zig backend)
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
#define PY_SSIZE_T_CLEAN
|
|
7
|
+
#include <Python.h>
|
|
8
|
+
|
|
9
|
+
// External Zig functions from libsatya - COMPREHENSIVE VALIDATORS
|
|
10
|
+
// Basic validators
|
|
11
|
+
extern int satya_validate_int(long value, long min, long max);
|
|
12
|
+
extern int satya_validate_string_length(const char* str, size_t min_len, size_t max_len);
|
|
13
|
+
extern int satya_validate_email(const char* str);
|
|
14
|
+
|
|
15
|
+
// String validators (Zod-style)
|
|
16
|
+
extern int satya_validate_url(const char* str);
|
|
17
|
+
extern int satya_validate_uuid(const char* str);
|
|
18
|
+
extern int satya_validate_ipv4(const char* str);
|
|
19
|
+
extern int satya_validate_base64(const char* str);
|
|
20
|
+
extern int satya_validate_iso_date(const char* str);
|
|
21
|
+
extern int satya_validate_iso_datetime(const char* str);
|
|
22
|
+
extern int satya_validate_contains(const char* str, const char* substring);
|
|
23
|
+
extern int satya_validate_starts_with(const char* str, const char* prefix);
|
|
24
|
+
extern int satya_validate_ends_with(const char* str, const char* suffix);
|
|
25
|
+
|
|
26
|
+
// Number validators (Pydantic-style)
|
|
27
|
+
extern int satya_validate_int_gt(long value, long min);
|
|
28
|
+
extern int satya_validate_int_gte(long value, long min);
|
|
29
|
+
extern int satya_validate_int_lt(long value, long max);
|
|
30
|
+
extern int satya_validate_int_lte(long value, long max);
|
|
31
|
+
extern int satya_validate_int_positive(long value);
|
|
32
|
+
extern int satya_validate_int_non_negative(long value);
|
|
33
|
+
extern int satya_validate_int_negative(long value);
|
|
34
|
+
extern int satya_validate_int_non_positive(long value);
|
|
35
|
+
extern int satya_validate_int_multiple_of(long value, long divisor);
|
|
36
|
+
|
|
37
|
+
// Float validators
|
|
38
|
+
extern int satya_validate_float_gt(double value, double min);
|
|
39
|
+
extern int satya_validate_float_finite(double value);
|
|
40
|
+
|
|
41
|
+
// Python wrapper: validate_int(value, min, max) -> bool
|
|
42
|
+
static PyObject* py_validate_int(PyObject* self, PyObject* args) {
|
|
43
|
+
long value, min, max;
|
|
44
|
+
|
|
45
|
+
if (!PyArg_ParseTuple(args, "lll", &value, &min, &max)) {
|
|
46
|
+
return NULL;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
int result = satya_validate_int(value, min, max);
|
|
50
|
+
return PyBool_FromLong(result);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Python wrapper: validate_string_length(str, min_len, max_len) -> bool
|
|
54
|
+
static PyObject* py_validate_string_length(PyObject* self, PyObject* args) {
|
|
55
|
+
const char* str;
|
|
56
|
+
Py_ssize_t min_len, max_len;
|
|
57
|
+
|
|
58
|
+
if (!PyArg_ParseTuple(args, "snn", &str, &min_len, &max_len)) {
|
|
59
|
+
return NULL;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
int result = satya_validate_string_length(str, (size_t)min_len, (size_t)max_len);
|
|
63
|
+
return PyBool_FromLong(result);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Python wrapper: validate_email(str) -> bool
|
|
67
|
+
static PyObject* py_validate_email(PyObject* self, PyObject* args) {
|
|
68
|
+
const char* str;
|
|
69
|
+
|
|
70
|
+
if (!PyArg_ParseTuple(args, "s", &str)) {
|
|
71
|
+
return NULL;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
int result = satya_validate_email(str);
|
|
75
|
+
return PyBool_FromLong(result);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Validator type enum for fast dispatch
|
|
79
|
+
enum ValidatorType {
|
|
80
|
+
VAL_INT = 0,
|
|
81
|
+
VAL_INT_GT,
|
|
82
|
+
VAL_INT_GTE,
|
|
83
|
+
VAL_INT_LT,
|
|
84
|
+
VAL_INT_LTE,
|
|
85
|
+
VAL_INT_POSITIVE,
|
|
86
|
+
VAL_INT_NON_NEGATIVE,
|
|
87
|
+
VAL_INT_MULTIPLE_OF,
|
|
88
|
+
VAL_STRING,
|
|
89
|
+
VAL_EMAIL,
|
|
90
|
+
VAL_URL,
|
|
91
|
+
VAL_UUID,
|
|
92
|
+
VAL_IPV4,
|
|
93
|
+
VAL_BASE64,
|
|
94
|
+
VAL_ISO_DATE,
|
|
95
|
+
VAL_ISO_DATETIME,
|
|
96
|
+
VAL_UNKNOWN
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Convert string to enum (do this ONCE, not per item!)
|
|
100
|
+
static enum ValidatorType parse_validator_type(const char* type_str) {
|
|
101
|
+
// Use first char for fast dispatch
|
|
102
|
+
switch (type_str[0]) {
|
|
103
|
+
case 'i':
|
|
104
|
+
if (strcmp(type_str, "int") == 0) return VAL_INT;
|
|
105
|
+
if (strcmp(type_str, "int_gt") == 0) return VAL_INT_GT;
|
|
106
|
+
if (strcmp(type_str, "int_gte") == 0) return VAL_INT_GTE;
|
|
107
|
+
if (strcmp(type_str, "int_lt") == 0) return VAL_INT_LT;
|
|
108
|
+
if (strcmp(type_str, "int_lte") == 0) return VAL_INT_LTE;
|
|
109
|
+
if (strcmp(type_str, "int_positive") == 0) return VAL_INT_POSITIVE;
|
|
110
|
+
if (strcmp(type_str, "int_non_negative") == 0) return VAL_INT_NON_NEGATIVE;
|
|
111
|
+
if (strcmp(type_str, "int_multiple_of") == 0) return VAL_INT_MULTIPLE_OF;
|
|
112
|
+
if (strcmp(type_str, "ipv4") == 0) return VAL_IPV4;
|
|
113
|
+
if (strcmp(type_str, "iso_date") == 0) return VAL_ISO_DATE;
|
|
114
|
+
if (strcmp(type_str, "iso_datetime") == 0) return VAL_ISO_DATETIME;
|
|
115
|
+
break;
|
|
116
|
+
case 's':
|
|
117
|
+
if (strcmp(type_str, "string") == 0) return VAL_STRING;
|
|
118
|
+
break;
|
|
119
|
+
case 'e':
|
|
120
|
+
if (strcmp(type_str, "email") == 0) return VAL_EMAIL;
|
|
121
|
+
break;
|
|
122
|
+
case 'u':
|
|
123
|
+
if (strcmp(type_str, "url") == 0) return VAL_URL;
|
|
124
|
+
if (strcmp(type_str, "uuid") == 0) return VAL_UUID;
|
|
125
|
+
break;
|
|
126
|
+
case 'b':
|
|
127
|
+
if (strcmp(type_str, "base64") == 0) return VAL_BASE64;
|
|
128
|
+
break;
|
|
129
|
+
}
|
|
130
|
+
return VAL_UNKNOWN;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
// Field spec with pre-parsed validator type AND cached PyObject
|
|
134
|
+
struct FieldSpec {
|
|
135
|
+
PyObject* field_name_obj; // Cached PyObject* for fast dict lookup
|
|
136
|
+
const char* field_name;
|
|
137
|
+
enum ValidatorType validator_type;
|
|
138
|
+
long param1;
|
|
139
|
+
long param2;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
// OPTIMIZED: validate_batch_direct with enum dispatch
|
|
143
|
+
static PyObject* py_validate_batch_direct(PyObject* self, PyObject* args) {
|
|
144
|
+
PyObject* items_list;
|
|
145
|
+
PyObject* field_specs_dict;
|
|
146
|
+
|
|
147
|
+
if (!PyArg_ParseTuple(args, "O!O!",
|
|
148
|
+
&PyList_Type, &items_list,
|
|
149
|
+
&PyDict_Type, &field_specs_dict)) {
|
|
150
|
+
return NULL;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
Py_ssize_t count = PyList_Size(items_list);
|
|
154
|
+
if (count == 0) {
|
|
155
|
+
return Py_BuildValue("([]i)", 0);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Pre-process field specs (convert strings to enums ONCE!)
|
|
159
|
+
Py_ssize_t num_fields = PyDict_Size(field_specs_dict);
|
|
160
|
+
struct FieldSpec* field_specs = malloc(num_fields * sizeof(struct FieldSpec));
|
|
161
|
+
if (!field_specs) {
|
|
162
|
+
return PyErr_NoMemory();
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
PyObject *field_name, *spec;
|
|
166
|
+
Py_ssize_t pos = 0;
|
|
167
|
+
Py_ssize_t field_idx = 0;
|
|
168
|
+
|
|
169
|
+
while (PyDict_Next(field_specs_dict, &pos, &field_name, &spec)) {
|
|
170
|
+
field_specs[field_idx].field_name_obj = field_name; // Cache PyObject* (borrowed ref)
|
|
171
|
+
field_specs[field_idx].field_name = PyUnicode_AsUTF8(field_name);
|
|
172
|
+
|
|
173
|
+
if (PyTuple_Check(spec) && PyTuple_Size(spec) >= 1) {
|
|
174
|
+
const char* type_str = PyUnicode_AsUTF8(PyTuple_GET_ITEM(spec, 0));
|
|
175
|
+
field_specs[field_idx].validator_type = parse_validator_type(type_str);
|
|
176
|
+
|
|
177
|
+
// Extract params (do this once, not per item!)
|
|
178
|
+
field_specs[field_idx].param1 = 0;
|
|
179
|
+
field_specs[field_idx].param2 = 0;
|
|
180
|
+
if (PyTuple_Size(spec) >= 2) {
|
|
181
|
+
field_specs[field_idx].param1 = PyLong_AsLong(PyTuple_GET_ITEM(spec, 1));
|
|
182
|
+
}
|
|
183
|
+
if (PyTuple_Size(spec) >= 3) {
|
|
184
|
+
field_specs[field_idx].param2 = PyLong_AsLong(PyTuple_GET_ITEM(spec, 2));
|
|
185
|
+
}
|
|
186
|
+
} else {
|
|
187
|
+
field_specs[field_idx].validator_type = VAL_UNKNOWN;
|
|
188
|
+
}
|
|
189
|
+
field_idx++;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Allocate results array
|
|
193
|
+
unsigned char* results = malloc(count * sizeof(unsigned char));
|
|
194
|
+
if (!results) {
|
|
195
|
+
free(field_specs);
|
|
196
|
+
return PyErr_NoMemory();
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
// Initialize all as valid
|
|
200
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
201
|
+
results[i] = 1;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
size_t valid_count = count;
|
|
205
|
+
|
|
206
|
+
// Iterate through each item and validate all fields (OPTIMIZED with enum dispatch)
|
|
207
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
208
|
+
PyObject* item = PyList_GET_ITEM(items_list, i); // Borrowed ref
|
|
209
|
+
|
|
210
|
+
// Prefetch next item for better cache performance
|
|
211
|
+
if (i + 1 < count) {
|
|
212
|
+
__builtin_prefetch(PyList_GET_ITEM(items_list, i + 1), 0, 3);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Fast dict check with branch prediction hint (usually true)
|
|
216
|
+
if (__builtin_expect(!PyDict_Check(item), 0)) {
|
|
217
|
+
free(field_specs);
|
|
218
|
+
free(results);
|
|
219
|
+
PyErr_SetString(PyExc_TypeError, "Expected list of dicts");
|
|
220
|
+
return NULL;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
// Iterate through pre-parsed field specs (ULTRA-FAST: use cached PyObject*)
|
|
224
|
+
for (Py_ssize_t f = 0; f < num_fields; f++) {
|
|
225
|
+
// Use PyDict_GetItem with cached PyObject* - FASTEST (borrowed ref, no refcount overhead)
|
|
226
|
+
PyObject* field_value = PyDict_GetItem(item, field_specs[f].field_name_obj);
|
|
227
|
+
|
|
228
|
+
if (!field_value) {
|
|
229
|
+
// Missing field
|
|
230
|
+
if (results[i] == 1) {
|
|
231
|
+
results[i] = 0;
|
|
232
|
+
valid_count--;
|
|
233
|
+
}
|
|
234
|
+
break; // Missing field, skip remaining validations
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Fast dispatch using switch/case (NO string comparisons!)
|
|
238
|
+
int is_valid = 1;
|
|
239
|
+
|
|
240
|
+
switch (field_specs[f].validator_type) {
|
|
241
|
+
case VAL_INT: {
|
|
242
|
+
long value = PyLong_AsLong(field_value);
|
|
243
|
+
is_valid = satya_validate_int(value, field_specs[f].param1, field_specs[f].param2);
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
case VAL_INT_GT: {
|
|
247
|
+
long value = PyLong_AsLong(field_value);
|
|
248
|
+
is_valid = satya_validate_int_gt(value, field_specs[f].param1);
|
|
249
|
+
break;
|
|
250
|
+
}
|
|
251
|
+
case VAL_INT_GTE: {
|
|
252
|
+
long value = PyLong_AsLong(field_value);
|
|
253
|
+
is_valid = satya_validate_int_gte(value, field_specs[f].param1);
|
|
254
|
+
break;
|
|
255
|
+
}
|
|
256
|
+
case VAL_INT_LT: {
|
|
257
|
+
long value = PyLong_AsLong(field_value);
|
|
258
|
+
is_valid = satya_validate_int_lt(value, field_specs[f].param1);
|
|
259
|
+
break;
|
|
260
|
+
}
|
|
261
|
+
case VAL_INT_LTE: {
|
|
262
|
+
long value = PyLong_AsLong(field_value);
|
|
263
|
+
is_valid = satya_validate_int_lte(value, field_specs[f].param1);
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
case VAL_INT_POSITIVE: {
|
|
267
|
+
long value = PyLong_AsLong(field_value);
|
|
268
|
+
is_valid = satya_validate_int_positive(value);
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case VAL_INT_NON_NEGATIVE: {
|
|
272
|
+
long value = PyLong_AsLong(field_value);
|
|
273
|
+
is_valid = satya_validate_int_non_negative(value);
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
case VAL_INT_MULTIPLE_OF: {
|
|
277
|
+
long value = PyLong_AsLong(field_value);
|
|
278
|
+
is_valid = satya_validate_int_multiple_of(value, field_specs[f].param1);
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
case VAL_STRING: {
|
|
282
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
283
|
+
is_valid = satya_validate_string_length(value, (size_t)field_specs[f].param1, (size_t)field_specs[f].param2);
|
|
284
|
+
break;
|
|
285
|
+
}
|
|
286
|
+
case VAL_EMAIL: {
|
|
287
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
288
|
+
is_valid = satya_validate_email(value);
|
|
289
|
+
break;
|
|
290
|
+
}
|
|
291
|
+
case VAL_URL: {
|
|
292
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
293
|
+
is_valid = satya_validate_url(value);
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
case VAL_UUID: {
|
|
297
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
298
|
+
is_valid = satya_validate_uuid(value);
|
|
299
|
+
break;
|
|
300
|
+
}
|
|
301
|
+
case VAL_IPV4: {
|
|
302
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
303
|
+
is_valid = satya_validate_ipv4(value);
|
|
304
|
+
break;
|
|
305
|
+
}
|
|
306
|
+
case VAL_BASE64: {
|
|
307
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
308
|
+
is_valid = satya_validate_base64(value);
|
|
309
|
+
break;
|
|
310
|
+
}
|
|
311
|
+
case VAL_ISO_DATE: {
|
|
312
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
313
|
+
is_valid = satya_validate_iso_date(value);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
case VAL_ISO_DATETIME: {
|
|
317
|
+
const char* value = PyUnicode_AsUTF8(field_value);
|
|
318
|
+
is_valid = satya_validate_iso_datetime(value);
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case VAL_UNKNOWN:
|
|
322
|
+
default:
|
|
323
|
+
is_valid = 1; // Skip unknown validators
|
|
324
|
+
break;
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
// Update result if invalid (FAST: branch prediction - valid is common case)
|
|
328
|
+
if (__builtin_expect(!is_valid, 0)) { // Hint: validation usually succeeds
|
|
329
|
+
if (results[i] == 1) {
|
|
330
|
+
results[i] = 0;
|
|
331
|
+
valid_count--;
|
|
332
|
+
}
|
|
333
|
+
break; // Already invalid, skip remaining validations
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// Convert results to Python list (FAST: use singleton bools, no allocations!)
|
|
339
|
+
PyObject* result_list = PyList_New(count);
|
|
340
|
+
for (Py_ssize_t i = 0; i < count; i++) {
|
|
341
|
+
PyObject* bool_obj = results[i] ? Py_True : Py_False;
|
|
342
|
+
Py_INCREF(bool_obj); // Must incref singleton
|
|
343
|
+
PyList_SET_ITEM(result_list, i, bool_obj);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
// Cleanup
|
|
347
|
+
free(field_specs);
|
|
348
|
+
free(results);
|
|
349
|
+
|
|
350
|
+
// Return (results, valid_count)
|
|
351
|
+
return Py_BuildValue("(Ni)", result_list, (Py_ssize_t)valid_count);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// Method definitions
|
|
355
|
+
static PyMethodDef DhiNativeMethods[] = {
|
|
356
|
+
{"validate_int", py_validate_int, METH_VARARGS,
|
|
357
|
+
"Validate integer bounds (value, min, max) -> bool"},
|
|
358
|
+
{"validate_string_length", py_validate_string_length, METH_VARARGS,
|
|
359
|
+
"Validate string length (str, min_len, max_len) -> bool"},
|
|
360
|
+
{"validate_email", py_validate_email, METH_VARARGS,
|
|
361
|
+
"Validate email format (str) -> bool"},
|
|
362
|
+
{"validate_batch_direct", py_validate_batch_direct, METH_VARARGS,
|
|
363
|
+
"GENERAL batch validation: (items, field_specs) -> (list[bool], int)"},
|
|
364
|
+
{NULL, NULL, 0, NULL}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
// Module definition
|
|
368
|
+
static struct PyModuleDef dhi_native_module = {
|
|
369
|
+
PyModuleDef_HEAD_INIT,
|
|
370
|
+
"_dhi_native",
|
|
371
|
+
"Native Zig validators for dhi (CPython extension)",
|
|
372
|
+
-1,
|
|
373
|
+
DhiNativeMethods
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
// Module initialization
|
|
377
|
+
PyMODINIT_FUNC PyInit__dhi_native(void) {
|
|
378
|
+
return PyModule_Create(&dhi_native_module);
|
|
379
|
+
}
|