MCPcopy Create free account
hub / github.com/documentdb/documentdb / ParseRequired

Function ParseRequired

pg_documentdb/src/jsonschema/bson_json_schema_tree.c:455–520  ·  view source on GitHub ↗

* This function reads the schema value for "required" keyword. * And stores it in the parent Node's Object validations section. */

Source from the content-addressed store, hash-verified

453 * And stores it in the parent Node's Object validations section.
454 */
455static void
456ParseRequired(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);

Callers 1

Calls 11

BsonTypeNameFunction · 0.85
PgbsonWriterInitFunction · 0.85
PgbsonWriterStartArrayFunction · 0.85
BsonValueInitIteratorFunction · 0.85
CreateStringViewHashSetFunction · 0.85
BsonIterTypeNameFunction · 0.85
PgbsonWriterFreeFunction · 0.85
PgbsonWriterEndArrayFunction · 0.85

Tested by

no test coverage detected