|
cwist
Modern C web framework with libttak runtime
|
Implementation of declarative schema validation and binding. More...
#include <cwist/core/validation/bind.h>#include <cwist/core/mem/alloc.h>#include <string.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <math.h>
Go to the source code of this file.
Functions | |
| bool | cwist_app_req_bind_json (cwist_http_request *req, const cwist_bind_schema_t *schema, void *out, cwist_bind_result_t *result) |
| Validate and bind a JSON request body into a struct per the schema. | |
| bool | cwist_app_req_bind_form (cwist_http_request *req, const cwist_bind_schema_t *schema, void *out, cwist_bind_result_t *result) |
| Validate and bind a form-encoded (key=value) request body into a struct per the schema. | |
| void | cwist_bind_write_error_response (cwist_http_response *res, const cwist_bind_result_t *result) |
| Write a 400 Bad Request JSON error response from a bind result. | |
| bool | cwist_app_req_bind_json_or_400 (cwist_http_request *req, cwist_http_response *res, const cwist_bind_schema_t *schema, void *out) |
| Bind a JSON request body, writing a 400 error response on failure. | |
Implementation of declarative schema validation and binding.
Definition in file bind.c.
| bool cwist_app_req_bind_form | ( | cwist_http_request * | req, |
| const cwist_bind_schema_t * | schema, | ||
| void * | out, | ||
| cwist_bind_result_t * | result | ||
| ) |
Validate and bind a form-encoded (key=value) request body into a struct per the schema.
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 | ||
| ) |
Validate and bind a JSON request body into a struct per the schema.
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 | ||
| ) |
Bind a JSON request body, writing a 400 error response on failure.
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 | ||
| ) |
Write a 400 Bad Request JSON error response from a bind result.
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().