|
cwist
Modern C web framework with libttak runtime
|
Declarative Schema Validation and Type-Safe Binding for CWIST. More...
#include <cwist/net/http/http.h>#include <cwist/core/sstring/sstring.h>#include <cwist/core/utils/json_builder.h>#include <cjson/cJSON.h>#include <stdbool.h>#include <stddef.h>#include <stdint.h>#include <regex.h>

Go to the source code of this file.
Classes | |
| struct | cwist_bind_rule |
| Single validation rule attached to a field. More... | |
| struct | cwist_bind_field |
| Per-field binding descriptor. More... | |
| struct | cwist_bind_schema |
| Complete schema descriptor for a struct. More... | |
| struct | cwist_bind_error |
| A single bind/validation failure. More... | |
| struct | cwist_bind_result |
| Result of a bind operation. More... | |
Typedefs | |
| typedef enum cwist_bind_rule_type | cwist_bind_rule_type_t |
| Supported validation constraint types. | |
| typedef struct cwist_bind_rule | cwist_bind_rule_t |
| Single validation rule attached to a field. | |
| typedef enum cwist_bind_field_type | cwist_bind_field_type_t |
| Logical type of a bind target field. | |
| typedef struct cwist_bind_field | cwist_bind_field_t |
| Per-field binding descriptor. | |
| typedef struct cwist_bind_schema | cwist_bind_schema_t |
| Complete schema descriptor for a struct. | |
| typedef struct cwist_bind_error | cwist_bind_error_t |
| A single bind/validation failure. | |
| typedef struct cwist_bind_result | cwist_bind_result_t |
| Result of a bind operation. | |
Enumerations | |
| enum | cwist_bind_rule_type { CWIST_BIND_RULE_REQUIRED , CWIST_BIND_RULE_MIN_LEN , CWIST_BIND_RULE_MAX_LEN , CWIST_BIND_RULE_MIN_VAL , CWIST_BIND_RULE_MAX_VAL , CWIST_BIND_RULE_REGEX , CWIST_BIND_RULE_EMAIL , CWIST_BIND_RULE_CUSTOM } |
| Supported validation constraint types. More... | |
| enum | cwist_bind_field_type { CWIST_BIND_BOOL , CWIST_BIND_INT , CWIST_BIND_UINT , CWIST_BIND_FLOAT , CWIST_BIND_DOUBLE , CWIST_BIND_STRING , CWIST_BIND_SSTRING , CWIST_BIND_JSON_OBJECT } |
| Logical type of a bind target field. More... | |
Functions | |
| bool | cwist_app_req_bind_json (cwist_http_request *req, const cwist_bind_schema_t *schema, void *out, cwist_bind_result_t *result) |
| Bind a JSON payload to a user struct according to a schema. | |
| bool | cwist_app_req_bind_form (cwist_http_request *req, const cwist_bind_schema_t *schema, void *out, cwist_bind_result_t *result) |
| Bind URL-encoded form data to a user struct. | |
| void | cwist_bind_write_error_response (cwist_http_response *res, const cwist_bind_result_t *result) |
| Populate a response with 400 Bad Request and a JSON error payload. | |
| bool | cwist_app_req_bind_json_or_400 (cwist_http_request *req, cwist_http_response *res, const cwist_bind_schema_t *schema, void *out) |
| Convenience wrapper: bind JSON and auto-respond 400 on failure. | |
Declarative Schema Validation and Type-Safe Binding for CWIST.
Provides a _Generic-driven dispatch layer that maps JSON/Form payloads directly into user-defined C structures with zero-copy semantics where possible. Validation rules (REQUIRED, MIN/MAX, REGEX, EMAIL) are evaluated at bind-time and automatically produce 400 Bad Request responses with descriptive JSON error payloads on failure.
Definition in file bind.h.
| #define CWIST_BIND_FIELD | ( | StructType, | |
| MemberName, | |||
| JsonKey, | |||
| RulesArray | |||
| ) |
Helper to declare a field descriptor with automatic type inference.
| StructType | The C struct type being bound (e.g., struct user). |
| MemberName | The member name inside the struct (e.g., email). |
| JsonKey | The JSON key that maps to this member. |
| RulesArray | A cwist_bind_rule_t array created with CWIST_BIND_RULES. |
Example:
| #define CWIST_BIND_RULES | ( | name, | |
| ... | |||
| ) | static const cwist_bind_rule_t name[] = {__VA_ARGS__, {-1, {0}, NULL}} |
Declare a static rule array suitable for use with CWIST_BIND_FIELD.
| name | C identifier for the array. |
| ... | cwist_bind_rule_t initialiser list. |
Example:
| #define CWIST_BIND_SCHEMA | ( | StructType, | |
| FieldArray | |||
| ) |
Build a schema descriptor from an array of field descriptors.
Internal helper that resolves the cwist_bind_field_type_t for a C expression.
Uses C11 _Generic to map common C types to the binding enum. This is the core mechanism that makes the macro API type-safe.
| #define CWIST_RULE_EMAIL | ( | ) |
| #define CWIST_RULE_REQUIRED | ( | ) |
A single bind/validation failure.
Per-field binding descriptor.
Logical type of a bind target field.
Result of a bind operation.
Single validation rule attached to a field.
Supported validation constraint types.
Complete schema descriptor for a struct.
Logical type of a bind target field.
| Enumerator | |
|---|---|
| CWIST_BIND_BOOL | |
| CWIST_BIND_INT | |
| CWIST_BIND_UINT | |
| CWIST_BIND_FLOAT | |
| CWIST_BIND_DOUBLE | |
| CWIST_BIND_STRING | Null-terminated C string (copied). |
| CWIST_BIND_SSTRING | cwist_sstring target. |
| CWIST_BIND_JSON_OBJECT | cJSON * (ownership transferred to caller). |
Supported validation constraint types.
| bool cwist_app_req_bind_form | ( | cwist_http_request * | req, |
| const cwist_bind_schema_t * | schema, | ||
| void * | out, | ||
| cwist_bind_result_t * | result | ||
| ) |
Bind URL-encoded form data to a user struct.
Operates on req->body treating it as application/x-www-form-urlencoded.
| req | HTTP request whose body contains the form payload. |
| schema | Bind schema describing the target struct layout. |
| out | Pointer to the user struct to populate. |
| result | [out] Detailed validation result. |
Bind URL-encoded form data to a user struct.
Uses a simple key=value scan of the body without URL unescaping; values are bounded to an internal buffer before validation.
| req | HTTP request whose body contains form text; may be NULL. |
| schema | Declarative field schema; must not be NULL. |
| out | Destination struct; must not be NULL. |
| result | Accumulator for validation errors; reset at start. |
| true | Result is valid and out is fully written. |
| false | result contains one or more field errors. |
Definition at line 446 of file bind.c.
References cwist_http_request::body, cap, and cwist_sstring::data.
| bool cwist_app_req_bind_json | ( | cwist_http_request * | req, |
| const cwist_bind_schema_t * | schema, | ||
| void * | out, | ||
| cwist_bind_result_t * | result | ||
| ) |
Bind a JSON payload to a user struct according to a schema.
The JSON body of req is parsed once and then each field in schema is extracted, validated, and written into out at the specified offset.
On validation failure the function returns false and populates result with per-field error messages. The caller can then pass result to cwist_bind_write_error_response() to emit a 400 response.
| req | HTTP request whose body contains the JSON payload. |
| schema | Bind schema describing the target struct layout. |
| out | Pointer to the user struct to populate. |
| result | [out] Detailed validation result. |
Bind a JSON payload to a user struct according to a schema.
Each schema field is looked up in the parsed JSON body, validated against its rules, and converted into the struct field at f->target_offset. The output struct is written even for fields with no matching key (zero values are written for missing fields).
| req | HTTP request whose body contains JSON text; may be NULL. |
| schema | Declarative field schema; must not be NULL. |
| out | Destination struct; must not be NULL. |
| result | Accumulator for validation errors; reset at start. |
| true | Result is valid and out is fully written. |
| false | result contains one or more field errors. |
Definition at line 426 of file bind.c.
References cwist_http_request::body, cap, and cwist_sstring::data.
Referenced by cwist_app_req_bind_json_or_400().
| bool cwist_app_req_bind_json_or_400 | ( | cwist_http_request * | req, |
| cwist_http_response * | res, | ||
| const cwist_bind_schema_t * | schema, | ||
| void * | out | ||
| ) |
Convenience wrapper: bind JSON and auto-respond 400 on failure.
If binding succeeds, the function returns true and out is filled. If binding fails, the function automatically sets res to 400 Bad Request with a JSON error body and returns false.
| req | HTTP request. |
| res | HTTP response (may be mutated on failure). |
| schema | Bind schema. |
| out | Pointer to the user struct to populate. |
Convenience wrapper: bind JSON and auto-respond 400 on failure.
Convenience wrapper around cwist_app_req_bind_json() and cwist_bind_write_error_response() for handlers that want automatic error replies.
| req | HTTP request whose body contains JSON text; may be NULL. |
| res | Response receiving the error payload when binding fails. |
| schema | Declarative field schema; must not be NULL. |
| out | Destination struct; must not be NULL. |
| true | out is fully written, res untouched. |
| false | A 400 error response was written to res. |
Definition at line 502 of file bind.c.
References cwist_app_req_bind_json(), and cwist_bind_write_error_response().
| void cwist_bind_write_error_response | ( | cwist_http_response * | res, |
| const cwist_bind_result_t * | result | ||
| ) |
Populate a response with 400 Bad Request and a JSON error payload.
The payload follows the shape:
| res | Response object to populate. |
| result | Validation result produced by a failed bind call. |
Populate a response with 400 Bad Request and a JSON error payload.
Serializes the accumulated field errors as a JSON object { "success": false, "errors": [ { "field", "message" }, ... ] }, sets the response status and Content-Type, and takes ownership of freeing the temporary JSON builder.
| res | Response to fill in; ignored when NULL. |
| result | Bind result containing the errors; ignored when NULL. |
Definition at line 463 of file bind.c.
References cwist_http_response::body, cap, CWIST_HTTP_BAD_REQUEST, cwist_http_header_add(), cwist_json_add_bool(), cwist_json_add_string(), cwist_json_begin_array(), cwist_json_begin_object(), cwist_json_builder_create(), cwist_json_builder_destroy(), cwist_json_end_array(), cwist_json_end_object(), cwist_json_get_raw(), cwist_sstring_assign(), cwist_bind_result::error_count, cwist_bind_result::errors, cwist_http_response::headers, cwist_bind_error::key, cwist_bind_error::message, cwist_http_response::status_code, and cwist_http_response::status_text.
Referenced by cwist_app_req_bind_json_or_400().