Skip to content

Validation

phil/structured-data

Validator::validate(Graph $graph) checks every node — those at the top of the graph and the nested ones — and returns a Report.

$report = new Validator()->validate($graph);

foreach ($report->errors() as $issue) {
    echo $issue->message, "\n"; // https://example.com/e#event needs startDate.
}

Each type has its TypeRules: the properties a node of that type needs, an error when one lacks, and those it should have, a warning. An entry may be a list of which one property is enough. A type with a parent needs what its parent needs too, and a node of several types what each of them needs. A property both required and recommended is only required.

TypeNeedsShould have
WebSitename, urlinLanguage, publisher
WebPageurlname, inLanguage, isPartOf
FAQPage (a WebPage)mainEntity
Questionname, acceptedAnswer
Answertext
BreadcrumbListitemListElement
ListItemposition, nameitem
Organizationnameurl, logo
LocalBusiness (an Organization)addresstelephone, url, image, openingHoursSpecification, geo
Personnameurl, sameAs
Eventname, startDate, locationendDate, eventStatus, description, image, offers, organizer
Placeone of name, addressaddress
PostalAddressstreetAddress, addressLocality, addressRegion, postalCode, addressCountry
Productname, one of offers, review, aggregateRatingimage, description, sku, brand
Offerprice, priceCurrencyavailability, url
CreativeWorkname, author
Book (a CreativeWork)name, authorisbn, url, inLanguage, bookFormat
Article (a CreativeWork)headlineauthor, datePublished, dateModified, image, publisher
SoftwareSourceCode (a CreativeWork)name, codeRepositoryprogrammingLanguage, license, description, version
SoftwareApplicationnameapplicationCategory, operatingSystem, offers, softwareVersion
ImageObjectone of contentUrl, urlcaption, width, height, license, creator

The kinds the factories build need what their parent needs: Corporation, NGO, EducationalOrganization and PerformingGroup are organisations; Store, Restaurant, CafeOrCoffeeShop, BarOrPub and EntertainmentBusiness local businesses; BusinessEvent, EducationEvent, ExhibitionEvent, Festival, MusicEvent, SocialEvent, SportsEvent and TheaterEvent events; NewsArticle, BlogPosting and TechArticle articles; AboutPage, CollectionPage, ContactPage, ItemPage and ProfilePage pages; WebApplication, MobileApplication and VideoGame applications.

Values

PropertyCheckedCode
startDate, endDate, datePublished, dateModified, dateCreated, uploadDate, priceValidUntil, validFrom, validThrough, birthDate, foundingDateAn ISO 8601 date, or date and time with maybe seconds, fractions and an offset; a day that existsinvalid-date
url, logo, image, sameAs, codeRepository, contentUrl, thumbnailUrl, downloadUrl, installUrl, itemAn absolute HTTP or HTTPS address, read by phil/url as a browser reads it: a host name — no IP address, no single name —, no user name or password, no spacesinvalid-url
priceCurrencyThree capitals, as ISO 4217 writes a currencyinvalid-currency
inLanguageA BCP 47 taginvalid-language

Only texts are checked: a node in image or logo, a number, a LanguageString are left alone. Each wrong value is an error.

An address on localhost or under .localhost is an invalid-url too, since no search engine reaches it. A page validated in development or in tests allows them: new Validator(allowLocalhost: true), kept by withRules(). An IP address, http://127.0.0.1/, stays invalid.

References and types

A reference to a node the graph lacks is a warning, unresolved-reference: it may name a thing described elsewhere, or a node a package forgot to give. A node at the top of the graph without a type is a warning, untyped-node: no rule applies to it.

The report

MemberGives
$issuesEvery Issue, in the order of the graph
errors(), warnings()The issues of one Severity
isValid()Whether there is no error; warnings may remain
toArray()The issues as data, for JSON

An Issue holds its severity, its code — missing-property, missing-recommended, invalid-date, invalid-url, invalid-currency, invalid-language, unresolved-reference, untyped-node —, the node it is about — its @id, or its type and place, such as Event @graph[2], then › and the property for a nested node —, the property, and an English message.