how to define object type in extensions api json format

8 views
Skip to first unread message

Mike Frysinger

unread,
Jun 28, 2018, 1:15:04 AM6/28/18
to extensi...@chromium.org
i want to update one of the builtin extension APIs (terminalPrivate.openTerminalProcess) to accept an object of key:value pairs, and enforce that all the keys/values are strings.  i've looked through the schemas.md and json_schema_compiler but can't find a way to define this in the json.  i also tried to find existing APIs that did this but couldn't -- any that accept objects seem to define all the members/types directly (e.g. chrome.tabs.create(createProperties)) or accept objects with arbitrary property types (e.g. additionalProperties allows you to attach objects with any types embedded).

e.g. i want to allow objects like {"foo":"bar", "var":"blah"}, but block non-string values like {"foo": 1, "var": {}, "blah": []} etc...

is this possible ?  or do i have to define it as "object" and then do the validation by hand in the C++ code ?
-mike

Devlin Cronin

unread,
Jun 28, 2018, 3:08:39 PM6/28/18
to vap...@chromium.org, extensi...@chromium.org
There's currently no support for this at the schema level, so right now you'd be forced to validate by hand.  There's some prior art with specifying a list element type, but that's only for arrays, not objects.  We could potentially add support, but that's non-trivial, and I'm not sure it'd be worth it if we're only doing this in a single (private) API.

As a potential workaround, since this is a private API (and thus doesn't have to be all that pretty), you *could* just pass in two arrays (keys and values), and specify each list to be composed of strings.  Then the C++ validation is just "keys.length == values.length" rather than needing to parse and verify each object property.  It's not as eloquent as passing the key-value dictionary as an object, but it would work with minimal extra code.

--
You received this message because you are subscribed to the Google Groups "extensions-dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to extensions-de...@chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Mike Frysinger

unread,
Jun 29, 2018, 6:07:45 PM6/29/18
to rdevlin...@chromium.org, extensi...@chromium.org
thanks for verifying.  i was hopefully assuming that i was missing something obvious here because of my unfamiliarity with the codebase.

i'll try validating an object directly in C++ if only for my own edification.  and because it'd map better to the JS side where the thing i want to pass in is already in object form (which maps directly to the underlying representation as a key-value store).
-mike
Reply all
Reply to author
Forward
0 new messages