Can a tap config_jsonschema property be configured...
# singer-tap-development
r
Can a tap config_jsonschema property be configured to accept multiple types? Like a
int or str
or
str or object
?
r
You can extend
JSONTypeHelper
to create your own type with a JSON schema definition - this is what a lot of the inbuilt types do: https://github.com/meltano/sdk/blob/c6a893372d036a99da81ee9131b8b6e17f50e670/singer_sdk/typing.py#L423-L442
Copy code
from singer_sdk.typing import JSONTypeHelper

class StrOrIntType(JSONTypeHelper):

    # provide an implementation
    def type_dict(cls):
        return {"type": ["string", "integer"]}
r
Thanks, @Reuben (Matatika) for pointing me to this! What prompted this is that some users are using environment variables to pass along the tap configuration. In one of my taps, one option accepts an object but this comes in as a json string in the environment variables. I want to be able to have the option to accept either an object (from a json file) or a json encoded string. Would I just need to extend the ObjectType helper to allow this?
r
So the config JSON schema is just used to validate data types for user-supplied config. In your case, would that not just be a string to cover both cases of the path to a JSON file or a raw JSON string? You can differentiate between them by trying to
json.loads
the value - if no exception, then you've got your data already; otherwise you can assume it's a file path and load the file. Having said that, the above logic would lie in the tap code rather than in the config schema. If you really want to validate from JSON schema, the spec does support regex pattern matching if you wanted to validate a JSON-formatted string (e.g. starts and ends with
[]
or
{}
) - I know there is some URI type that is built into the SDK currently which could maybe work for a local path, but I'm not sure about that. If it turns out it's not so straightforward to implement, I would just do the first solution personally.