diff --git a/docs/configuration/schema.json b/docs/configuration/schema.json index 031ea48..cf0775f 100644 --- a/docs/configuration/schema.json +++ b/docs/configuration/schema.json @@ -1,62 +1,84 @@ { - "$schema": "http://json-schema.org/draft-07/schema#", - "$id": "https://raw.githubusercontent.com/gitmobkab/copia/main/docs/configuration/schema.json", - "title": "Copia Configuration", - "description": "Configuration schema for copia — https://gitmobkab.github.io/copia/configuration", - "type": "object", - "properties": { - "profiles": { - "type": "object", - "description": "Named database connection profiles.", - "additionalProperties": { - "$ref": "#/$defs/Profile" - } - } - }, - "additionalProperties": false, - "$defs": { - "Profile": { - "type": "object", - "description": "A database connection profile.", - "required": ["adapter", "host", "port", "database", "user"], - "additionalProperties": false, - "properties": { - "adapter": { - "type": "string", - "description": "The database adapter to use.", - "enum": ["mysql"], - "enumDescriptions": [ - "MySQL / MariaDB" - ] - }, - "host": { - "type": "string", - "description": "Database host. Accepts a hostname, IPv4, or IPv6 address.", - "examples": ["localhost", "127.0.0.1", "db.example.com"] - }, - "port": { - "type": "integer", - "description": "Database port. Must be between 1 and 65535.", - "minimum": 1, - "maximum": 65535, - "examples": [3306] - }, - "database": { - "type": "string", - "description": "Target database name. Must be non-empty and ASCII only.", - "minLength": 1 - }, - "user": { - "type": "string", - "description": "Database user. Must be non-empty and ASCII only.", - "minLength": 1 - }, - "password": { - "type": "string", - "description": "Database password. Optional — defaults to empty string.", - "default": "" + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://raw.githubusercontent.com/gitmobkab/copia/main/docs/configuration/schema.json", + "title": "Copia Configuration", + "description": "Configuration schema for copia - https://gitmobkab.github.io/copia/configuration", + "type": "object", + "properties": { + "profiles": { + "type": "object", + "description": "Named database connection profiles.", + "additionalProperties": { + "$ref": "#/$defs/Profile" + } + } + }, + "$defs": { + "Profile": { + "additionalProperties": false, + "properties": { + "adapter": { + "description": "The copia Adapter to use", + "enum": [ + "mysql", + "postgres" + ], + "title": "Adapter", + "type": "string", + "enumDescriptions": [ + "MySQL / MariaDB", + "PostgreSQL" + ] + }, + "host": { + "anyOf": [ + { + "format": "ipvanyaddress", + "type": "string" + }, + { + "minLength": 1, + "type": "string" + } + ], + "description": "The database hostname", + "title": "Host" + }, + "port": { + "description": "The Database Port", + "maximum": 65535, + "minimum": 1, + "title": "Port", + "type": "integer" + }, + "database": { + "description": "The Database Port", + "minLength": 1, + "title": "Database", + "type": "string" + }, + "user": { + "description": "The Database Username", + "minLength": 1, + "title": "User", + "type": "string" + }, + "password": { + "default": "", + "description": "The Database Password", + "title": "Password", + "type": "string" + } + }, + "required": [ + "adapter", + "host", + "port", + "database", + "user" + ], + "title": "Profile", + "type": "object" } - } } - } } \ No newline at end of file diff --git a/scripts/config_schema.py b/scripts/config_schema.py new file mode 100644 index 0000000..592f12b --- /dev/null +++ b/scripts/config_schema.py @@ -0,0 +1,33 @@ +import json +from copia.cli.config import Profile + +def generate_schema() -> dict: + profile_schema = Profile.model_json_schema() + + # inject enumDescriptions manually since it's non-standard + if "properties" in profile_schema: + adapter = profile_schema["properties"].get("adapter", {}) + if "enum" in adapter: + adapter["enumDescriptions"] = ["MySQL / MariaDB", "PostgreSQL"] + + return { + "$schema": "http://json-schema.org/draft-07/schema#", + "$id": "https://raw.githubusercontent.com/gitmobkab/copia/main/docs/configuration/schema.json", + "title": "Copia Configuration", + "description": "Configuration schema for copia - https://gitmobkab.github.io/copia/configuration", + "type": "object", + "properties": { + "profiles": { + "type": "object", + "description": "Named database connection profiles.", + "additionalProperties": {"$ref": "#/$defs/Profile"}, + } + }, + "$defs": {"Profile": profile_schema}, + } + +if __name__ == "__main__": + schema = generate_schema() + output = "docs/configuration/schema.json" + with open(output, "w") as f: + json.dump(schema, f, indent=4) \ No newline at end of file diff --git a/src/copia/cli/config/models.py b/src/copia/cli/config/models.py index d1ccef0..c88a33e 100644 --- a/src/copia/cli/config/models.py +++ b/src/copia/cli/config/models.py @@ -13,18 +13,19 @@ Port = Annotated[int, Field( ge= PORT_MIN_VAL, le= PORT_MAX_VAL, - strict=True + strict=True, + description="The Database Port" )] class Profile(BaseModel): model_config = ConfigDict(extra="forbid") - adapter: Adapter - host: Union[IPvAnyAddress, NotEmptyStr] + adapter: Adapter = Field(description="The copia Adapter to use") + host: Union[IPvAnyAddress, NotEmptyStr] = Field(description="The database hostname") port: Port - database: NotEmptyStr - user: NotEmptyStr - password: str = "" + database: NotEmptyStr = Field(description="The Database Port") + user: NotEmptyStr = Field(description="The Database Username") + password: str = Field("", description="The Database Password") def __str__(self) -> str: