google/schema-dts

SearchAction's potentialAction doesn't expect target (and should)

bsides opened this issue · 1 comments

The valid schema as defined here https://schema.org/SearchAction would not compile because target in potentialAction isn't properly specified. If you remove target it compiles.

{
    "@context": "http://schema.org",
    "@type": "WebSite",
    "url": "http://example.com/",
    "potentialAction": {
      "@type": "SearchAction",
      "target": "http://example.com/search?&q={query}",
      "query": "required"
    }
}

potentialAction is of type Action which receives SearchAction as type, which has only @type = 'SearchAction' and SearchActionBase, which finaly has only query: Text as type (which is a string). Unfortunately target is nowhere to be found in any of those. Is this on purpose? Did anything change that I don't know?

Thanks in advance

Eyas commented

Note that the type of target is defined as an EntryPoint. target exists on type Action and we have it in the schema.d.ts

Note that technically, all Schema.org entities can be written as a string and implicitly casted (discussed in #19, #21, and #37). While in Schema.org it is legal to define anything as a string, most search engines are more stringent, and accept a subset of that Schema.

schema-dts tends to only allow things tend to be accepted/required by search engines. This seems to be one of them:

Workaround

Setting an EntryPoint to string is equivalent to setting an EntryPoint object with that string as the urlTemplate. So, you can write your target as follows:

{
    "@context": "http://schema.org",
    "@type": "WebSite",
    "url": "http://example.com/",
    "potentialAction": {
      "@type": "SearchAction",
      "target": {
        "@type": "EntryPoint",
        "urlTemplate": "http://example.com/search?&q={query}"
      },
      "query": "required"
    }
}

The Fix.

EntryPoint should accept being typed as a string.