Closed pdehaan closed 4 years ago
Hi Peter!
That seems like a great use case! It's not currently supported, but based on this I'm happy to add support for both a new structuredDataTestJson
method and auto-detection if a JSON object (or array of objects) is passed. The command line tool should also support validating JSON input.
The approach you have should work in the interim (I did a quick check and works for me), as long as you have an await
before the function call:
async function lintFile(file, options = {}) {
const txt = fs.readFileSync(file, "utf-8").toString();
const html = `<script type="application/ld+json">${txt}</script>`;
return await structuredDataTestHtml(html, options);
}
If this doesn't work for you and/or if the docs need updating anywhere to reflect this would appreciate if you can feedback (happy to help)…
However, ticket #5 is still in progress, which means that for now testing this way is likely of limited usefulness without a custom preset to check the properties and values are valid.
If you have specific use cases, very happy to focus on those first if you want to elaborate!
This is what I get from the command line:
npx structured-data-testing-tool --file ./bedrock/base/templates/includes/structured-data/product/firefox-developer-product.json --schemas jsonld:Product
Tests
Schema.org > Product - 0% (0 passed, 1 total)
✕ schema in jsonld [Product[*]]
Statistics
Number of Metatags: 0
Schemas in JSON-LD: 0
Schemas in HTML: 0
Schema in RDFa: 0
Schema.org schemas: 0
Other schemas: 0
Test groups run : 1
Total tests run: 1
Results
Passed: 0 (0%)
Warnings: 0 (0%)
Failed: 1 (100%)
✕ 1 of 1 tests failed with 0 warnings.
And here's the output (no errors or warnings) when I try using the API:
node sdttt
./bedrock/base/templates/includes/structured-data/product/firefox-developer-product.json
[]
Thanks @pdehaan - sorry hadn't missed the notification for this. Are you able to share the JSON file so can investigate (either to fix a bug and/or improve the docs - though sounds like maybe both)?
Yeah, sorry. Looks like the [draft] PR is public; https://github.com/mozilla/bedrock/pull/7664
I think I tried experimenting w/ linting the JSON files directly, as well as wrapping the files in a <script type="application/ld+json">
tag and writing the file out as an .html and linting that.
Hi @pdehaan,
It's taken a while for me to get round to, but if it's of use to you or anyone else:
As of version 4.1 JSON input is now supported in URLs, files and strings / buffers / streams / etc in both the API and Command Line Interface.
It actually treats it as described by automatically wrapping it with an appropriate script tag when the input validates as serialized JSON, so that the validation is exactly the same and it's automatic so no additional code or options are required to test JSON files.
Although there is improve schema evaluation in version 4, actual Schema.org property validation is part of the milestone for for version 5 so this still may not be useful to you, but this still may be useful for other folks.
Hello,
Is there a way to use the API to validate a JSON/HTML file with the following data, or do I need to write a custom schema and rules?
Currently I'm using the following function and looping over a glob of .json files, but I'm getting zero warnings or failed rules: