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 presentation layer set in config. Validation errors are reported via CommandHelper.error (exits in production) and the method returns false so test doubles can observe failures without process termination.

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!.layer == null) {
    _commandHelper.error(
      'Schema is not valid. Exactly one of "config.bloc", "config.riverpod", '
      'or "config.getx" must be set to true.',
    );
    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;
}