* This function reads the schema value for "required" keyword. * And stores it in the parent Node's Object validations section. */
| 453 | * And stores it in the parent Node's Object validations section. |
| 454 | */ |
| 455 | static void |
| 456 | ParseRequired(const bson_value_t *value, SchemaNode *node) |
| 457 | { |
| 458 | if (value->value_type != BSON_TYPE_ARRAY) |
| 459 | { |
| 460 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_TYPEMISMATCH), |
| 461 | errmsg( |
| 462 | "Expected an array for 'required' in $jsonSchema, but got %s", |
| 463 | BsonTypeName(value->value_type)))); |
| 464 | } |
| 465 | pgbson_writer writer; |
| 466 | PgbsonWriterInit(&writer); |
| 467 | pgbson_array_writer arrayWriter; |
| 468 | PgbsonWriterStartArray(&writer, "", 0, &arrayWriter); |
| 469 | bson_iter_t iter; |
| 470 | BsonValueInitIterator(value, &iter); |
| 471 | |
| 472 | /* Hash table to track seen field names for efficient duplicate detection */ |
| 473 | HTAB *seenFieldsHash = CreateStringViewHashSet(); |
| 474 | |
| 475 | while (bson_iter_next(&iter)) |
| 476 | { |
| 477 | if (!BSON_ITER_HOLDS_UTF8(&iter)) |
| 478 | { |
| 479 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_TYPEMISMATCH), |
| 480 | errmsg( |
| 481 | "All elements in 'required' array must be strings, but encountered %s", |
| 482 | BsonIterTypeName(&iter)))); |
| 483 | } |
| 484 | const char *field = bson_iter_utf8(&iter, NULL); |
| 485 | |
| 486 | /* TODO: Add support for dot notation paths */ |
| 487 | if (strchr(field, '.') != NULL) |
| 488 | { |
| 489 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_FAILEDTOPARSE), |
| 490 | errmsg("Dot notation paths are currently not supported: %s", |
| 491 | field))); |
| 492 | } |
| 493 | |
| 494 | /* Check for duplicate field names using hash table */ |
| 495 | StringView fieldView = CreateStringViewFromString(field); |
| 496 | bool found; |
| 497 | hash_search(seenFieldsHash, &fieldView, HASH_ENTER, &found); |
| 498 | |
| 499 | if (found) |
| 500 | { |
| 501 | hash_destroy(seenFieldsHash); |
| 502 | PgbsonWriterFree(&writer); |
| 503 | ereport(ERROR, (errcode(ERRCODE_DOCUMENTDB_FAILEDTOPARSE), |
| 504 | errmsg( |
| 505 | "Duplicate field name in $jsonSchema 'required' array"))); |
| 506 | } |
| 507 | |
| 508 | PgbsonArrayWriterWriteUtf8(&arrayWriter, field); |
| 509 | } |
| 510 | |
| 511 | /* Clean up hash table */ |
| 512 | hash_destroy(seenFieldsHash); |
no test coverage detected