@didomi/consent-string-schema

中文文档
1.1.0发布于 2026/04/09TypeScriptESM + CJS近 30 天下载 1.6万
* [Consent String Schema](#consent-string-schema) * [Schema format](#schema-format) * [Type](#type) * [Field](#field) * [Consent String Type](#consent-string-type) * [Specification Version](#specification-version) * [Tes
原始使用文档
文档目录12 节
  1. Schema format
  2. Type
  3. Field
  4. Consent String Type
  5. Specification Version
  6. Test
  7. Types
  8. Fields and segments
  9. Schema Validation
  10. Structure validation
  11. Type validation
  12. Key validation

Consent String Schema

Consent strings store user consent status as binary data, encoded in base64. Encoding user consent status into a binary string, or decoding it from an existing one, requires an encoder/decoder library. The schema defines the consent string format in a human-readable way. This allows new formats to be defined, or existing ones updated, without modifying the encoder/decoder library.

Schema format

The schema has the following structure:

  • consent_string_type
  • specification_version
  • tests
  • types
  • fields or segments

Type

Encoders and decoders need to know the type of data they're working with. The schema defines fields with specific types, much like variables in programming. The following types must be supported by the encoder/decoder library:

#type iddescription
1u1Unsigned integer number of 1 bit size (0 or 1)
2u2Unsigned integer number of 2 bits size
3u3Unsigned integer number of 3 bits size
4u4Unsigned integer number of 4 bits size
5u6Unsigned integer number of 6 bits size
6u12Unsigned integer number of 12 bits size
7u16Unsigned integer number of 16 bits size
8u24Unsigned integer number of 24 bits size
9u32Unsigned integer number of 32 bits size
10dateDate
11uuidUser ID - Base16 string xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'
12fibonacciFibonacci Number
13fibonacci_rangeFibonacci range of ids with start_id and number_of_ids in Fibonacci numbers
14u16_rangeIds range with start_id and end_id as u16 numbers
15bit_fieldArray of bits
16fixed_bit_fieldFixed Size BitField array, size property must be specified in the schema
17bit_field_2_bitsBitField array of tuples (bool, bool)
18ranges_u16Array of id ranges - (start_id: u16, end_id: u16)
19ranges_fibonacciArray of id ranges - (start_id: fibonacci, number_of_ids: fibonacci)
20stringA fixed amount of bit representing a string. The character’s ASCII integer ID is subtracted by 65 and encoded into an int(6).
21optimized_rangeFirst data type is always a Boolean (u1) and if it is true then the next is Fibonacci range, if false - Bitfield.
22optimized_u16_rangeOptimized BitField with u16_range - (max_id: u12, range_flag: u1, type: [u16_range, fixed_bit_field(size: max_id)])
23array_of_optimized_u16_rangesArray of optimized_u16_range (number_of_records: u12, key: u6, type: u2, ids: optimized_u16_range)
24n_array_of_ranges_x_yGeneric type<X,Y> (number_of_records: u12, key: u, type: u, ids: optimized_range)
25optimized_array_of_u16_ranges(max_id: u16, is_range_encoding: u1, type: [(number_of_entries: u12, is_a_range: u1, type: [(start_id:u16, end_id: u16), start_id:u16]), fixed_bit_field(size: max_id)])
26array_of_u16_ranges(number_of_records: u12, key: u6, type: u2, number_of_entries: u12, is_a_range: u1, type: [(start_id:u16, end_id: u16), start_id:u16])
27segment_typeThe 3bits code of segment type
28enabled_disabled_idsThe enabled and disables ids encoded with some types listed in variants property
29array_of_attributed_u16_rangesThe array of attributed ranges used for publisher restriction encoding
30versionThe consent string version encoded in 6bits number

Field

The main building block of the schema is a field. The field is analog of variable in a programming language and each filed must have a type. There is a field definition example:

{
  "type": "version",
  "description": "The Didomi Consent String version 6bits number",
  "key": "dcs_version",
  "value": 2  
}

Here we are defining the field with type "version" and key equal to "dcs_version". The encoder will use the key property to access the field value provided in the user status object. We could also define the field value property to instruct the encoder to read the value not from the user status object but from the schema.

Another example of the field property is optional. Each field could be an optional and this will instruct encoder to place 1 bit flag in encoded string before the field value. If the flag is 1, the value is present and decoded. If it's 0, the value is absent and skipped during decoding. There is example of the optional field definition

{
  "type": "date",
  "key": "sync",
  "optional": true
}

There are fields that could be encoded using different encoding algorithms for optimization purposes. To specify what algorithms (types) could be used by encoder for value encoder we use variants field attribute:

{

  "type": "enabled_disabled_ids",
  "description": "User Status for purposes opt-out",
  "key": "purposes_li",
  "variants": [
    "bit_field_2_bits",
    "ranges_u16",
    "ranges_fibonacci"
  ]
}

This example shows how to encode user status for purposes using three methods: bit_field_2_bits, ranges_u16, and ranges_fibonacci. The encoder chooses the method producing the shortest result and includes the chosen method's code before the encoded value, allowing the decoder to correctly decode the string.

Some types need to have size information to encode / decode correctly.

{
  "type": "string",
  "description": "Two-letter ISO 639-1 language code in which the CMP UI was presented",
  "key": "consent_language",
  "size": 12
}

Here we are defining a 12 bits string type field.

There is the summary for all field attributes:

#field attributemandatorydescription
1typeYfield type
2keyYkey to extract value from the model
3descriptionYfield description
4sizeNtype size bits
5optionalNtrue if value could be missing
6valueNfield constant value
7variantsNtype encoding methods variants

The consent string schema supports the following types:

#string typedescription
1didomi_consent_stringDidomi Consent String
2iab_tcf_stringIAB TCF Consent String
3gpp_stringGPP String

Specification Version

For each string type several specification versions could be defined, for example, for Didomi Consent String we have specification_version 1 and 2, where version 1 has some restrictions for the encoder.

Test

The consent string schema defines tests for an encoder and decoder in tests array. The encoder/decoder library should run these tests to make sure the library produces expected results based on the schema.

Types

The types array defines a subset of types supported by the consent string schema (Type). Each type from types must appear at least ones in field definition and all types from fields definition should be listed in types array.

Fields and segments

The consent string schema could have fields array on the top level or segments array. The fields array contains field definition (Field) and segments could be used to define a segmented string structure, where each segment encoded separately and then connected to each other using . character.

For example:

{
  "tests": {
    "encoded": "CQH-gkAQH-gkAAHABBENBOFgAPAAAELAAAAAF5wAQF5gXnABAXmAAAAA.YAAAAAAAAAAA"
  }
}

Here we have an IAB TCF string as encoded value with two segments : core and publisher.

In the schema segments defined as the following object:

 {
    "name": "Disclosed Vendors",
    "key": "disclosed_vendors",
    "optional": true,
    "fields": [
      {
        "type": "segment_type",
        "description": "DisclosedVendors segment is 1 which is 001 in binary.",
        "key": "disclosed_vendors_segment_type",
        "value": 1
      },
      {
        "type": "optimized_array_of_u16_ranges",
        "description": "Vendors Disclosure",
        "key": "disclosed_vendors"
      }
    ]
  }

Each segment has name, key, optional and fields properties, where the all property except optional mandatory ones.

Schema Validation

The consent string schema library provides validator function that validates schemas using 3 steps:

  • validate schema structure using zod library
  • validate schema types
  • validate schema keys

Structure validation

We use zod to define the consent string schema structure and also control types. For example, there is field definition using zod:

import { z } from 'zod';

export const availableTypesSchema = z.nativeEnum(SchemaTypes);

export const typesSchema = uniqueNotEmptyArray(availableTypesSchema);

export const consentStringTypeSchema = z.enum([
  'didomi_consent_string',
  'iab_tcf_string',
  'gpp_string',
]);

export const variantsTypes = z.enum([SchemaTypes.bit_field_2_bits, SchemaTypes.ranges_fibonacci, SchemaTypes.ranges_u16]);

export const variantsSchema = uniqueNotEmptyArray(variantsTypes);

export const fieldSchema = z.object({
  type: availableTypesSchema,
  description: z.string(),
  key: z.string(),
  optional: z.boolean().optional(),
  size: z.number().or(z.string()).optional(),
  value: z.number().optional(),
  variants: variantsSchema.optional()
});

This schema ensures the type property contains only permitted types, and the variants property contains exactly three allowed types.

Type validation

While zod validates types, the schema must also ensure all types in its types array are used in field definitions, and that all field types are listed in the types array. This mirrors programming languages, where types are defined before use and unused definitions may trigger warnings or errors. The library's typeValidator enforces this constraint after zod validation.

Key validation

The last validation step need to ensure that all keys in the fields definition unique. The library's keyValidator runs after the typeValidator to enforces this constraint.

版本与发布记录

@didomi/consent-string-schema 共发布 1 个版本,首次发布于 2026/04/09;过去 12 个月发布了 1 个版本。

版本发布日期距今
1.1.0latest2026/04/097 个月前

关于 @didomi/consent-string-schema 的常见问题

@didomi/consent-string-schema 是什么?

* [Consent String Schema](#consent-string-schema) * [Schema format](#schema-format) * [Type](#type) * [Field](#field) * [Consent String Type](#consent-string-type) * [Specification Version](#specification-version) * [Tes

如何安装 @didomi/consent-string-schema?

在项目目录运行 npm install @didomi/consent-string-schema,或使用 pnpm add / yarn add / bun add。

@didomi/consent-string-schema 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@didomi/consent-string-schema 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@didomi/consent-string-schema 可以商用吗?

@didomi/consent-string-schema 使用 未声明 许可。package.json 没有声明许可证。没有许可证的代码默认保留全部权利,商用前请联系作者或查看仓库中的 LICENSE 文件。以上为概括,不构成法律意见,请以许可原文为准。

@didomi/consent-string-schema 的下载量有多少?

根据 npm 官方下载统计,@didomi/consent-string-schema 在 2026/10/12 – 2026/11/10 这 30 天内被下载 15,846 次,日均约 528 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@didomi/consent-string-schema 还在维护吗?

最新版本 1.1.0 发布于 2026/04/09(7 个月前);至今共发布 1 个版本,其中过去 12 个月发布了 1 个;首次发布于 2026/04/09。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。