A2uiCatalogComponent.simple constructor

A2uiCatalogComponent.simple({
  1. required String name,
  2. String? description,
  3. required List<A2uiParam> params,
  4. bool checkable = false,
})

Builds a spec-shaped component from an ordered parameter list, for catalogs authored in Dart rather than loaded from a catalog.json.

A2uiCatalogComponent.simple(
  name: 'Gauge',
  description: 'A circular gauge for a single numeric value.',
  params: [
    A2uiParam.dynamicValue('value', required: true, type: .number),
    A2uiParam.number('max', description: 'Range maximum.'),
  ],
);

params order is the Express positional argument order, so put the most important values first. checkable adds the spec's checks property. Param names must be unique, and component and id are reserved.

Implementation

factory A2uiCatalogComponent.simple({
  required String name,
  String? description,
  required List<A2uiParam> params,
  bool checkable = false,
}) {
  // Thrown rather than asserted: a production build would otherwise ship a
  // schema where one param silently overwrites another (or the
  // `component` discriminator).
  if (params.map((p) => p.name).toSet().length != params.length) {
    throw ArgumentError.value(
      params.map((p) => p.name).toList(),
      'params',
      'A2uiCatalogComponent.simple($name): duplicate param names',
    );
  }
  if (params.any((p) => p.name == 'component' || p.name == 'id')) {
    throw ArgumentError.value(
      params.map((p) => p.name).toList(),
      'params',
      'A2uiCatalogComponent.simple($name): `component` and `id` are reserved',
    );
  }
  final properties = <String, dynamic>{
    'component': {'const': name},
    for (final p in params) p.name: p._schema(),
  };
  return A2uiCatalogComponent(
    name: name,
    schema: _frozen({
      'type': 'object',
      'description': ?description,
      if (checkable)
        'allOf': [
          {r'$ref': '$_commonTypes#/\$defs/Checkable'},
        ],
      'properties': properties,
      'required': [
        'component',
        for (final p in params)
          if (p.required) p.name,
      ],
    }),
  );
}