validateSchema method

bool validateSchema(
  1. Schema schema
)

Validates that schema has the required sections and config.

Requires api, api.methods, response, and config, with exactly one of config.bloc or config.riverpod set to true.

In multi-response mode, additionally checks that any method response key that is set corresponds to a declared response entity.

Implementation

bool validateSchema(Schema schema) {
  if (schema.api == null) {
    _commandHelper.error('Schema is not valid. "api" is required.');
    return false;
  }
  if (schema.api?.methods == null) {
    _commandHelper.error('Schema is not valid. "api.methods" is required.');
    return false;
  }
  if (schema.response == null && schema.responses == null) {
    _commandHelper.error('Schema is not valid. "response" is required.');
    return false;
  }
  if (schema.config == null) {
    _commandHelper.error('Schema is not valid. "config" is required.');
    return false;
  }
  if (schema.config!.bloc == null && schema.config!.riverpod == null) {
    _commandHelper.error('Schema is not valid. "config.bloc" or "config.riverpod" is required.');
    return false;
  }

  // In multi-response mode: warn if a method's response key is unknown.
  if (schema.isMultiResponse) {
    final validKeys = schema.responses!.keys.toSet();
    for (final method in schema.api!.methods!.method!.entries) {
      final resp = method.value.response;
      if (resp != null && !validKeys.contains(resp)) {
        _commandHelper.warning(
          'Method "${method.key}" declares response "$resp" which is not a key in the '
          '"response" section. Valid keys: ${validKeys.join(', ')}.',
        );
      }
    }
  }

  return true;
}