Replies: 1 comment 7 replies
-
This is the main thing I want to callout, we already pass I don't think we should be building a separate package to start with, if we have use-cases in Gutenberg/Core, we should start by just building this into the dataviews package. The one thing that I want to call out, is that I think we should implement this with a core use-case in mind ideally. So where can we use DataForm and schema validation today in Core? Some things that come to mind where we need something like that:
|
Beta Was this translation helpful? Give feedback.
-
TL;DR: This discussion proposes a schema validation package in Gutenberg, why we need this, why we're not using an external package, and what would the scope of it. I'm intentionally leaving Woo's use cases out of this but might reference them in passing.
Why we need JSON Schema validation package
Luigi and Lourens are working on a DataForms, a form generation package that takes a JSON object of field definition, and build a form out of it. Every form need validation, and validation need go beyond required and optional, and have several needs.
One approach to such validation is have a JS function at each field definition. This system works fine but is severely limited:
For this, @joshuatf proposed we move our validation logic to JSON Schema, a standard vastly adopted everywhere, including WordPress and WooCommerce. WordPress is no stranger to Schema and exposes its own validation system for REST API.
JSON Schema is is an industry standard, adopting it will give us a safe, future proof, system to validate data and potentially conditionally render things.
Example of JSON Schema to validate a set of fields
JSON Schema validator would take a form data object like:
And match it against user-defined schema:
And would return true, meaning there are no errors.
Or would take a faulty JSON object and return developer-oriented errors that should be mapped down to user-readable errors by consumers.
Consumers of
DataForm
would pass a separateschema
, or just do the validation themselves and pass anerrors
object, to be figured out.Why not use an external package
As it stands right now, there are some packages out there that implement schema validation, including the well known Ajv. Using an external package is not ruled out, but there are some limitations.
When it comes to public packages, they split into 2:
$data
keyword, which allows you to use JSON pointers too reference values and keys from your JSON, sort of "minimum of this field is maximum of that, require this if that is checked" kind of the circular pointers. Prime examples include Cloudflare worker's package, exodus schemasafe, hyperjump validator, and jema.js (very alpha package)Using an external package as is probably wouldn't work for us, because we want both. So ideally we want to have a WordPress level package (
@wordpress/validation
) that consumes one of the packages (we figure out a way to reduce Ajv size, we extend one of the above packages with $data, we only support a single draft), or build our own parser. Thankfully, building a JSON validator is not a technically hard problem, and plenty of unit tests exists to ensure whatever we build is up to spec.But I do think that a WordPress package is needed, part of it to enforce our own rules, add our own extensibility, and possibility some of WordPress sugar syntax (required is at properties level in WP schema, not at object level in drafts).
The scope of the package
as an initial problem, we want to support a single draft (draft 7), a limited set of keywords (whatever we conclude is needed to describe complex objects), for now, this is the scope we're looking to support:
Bonus: Conditional rendering
JSON Schema can also be used for conditional rendering, the logic to render or not would be to see if the schema returns errors or not. In conditional rendering, the equation is flipped, and you pass a schema against current document data, not your field, example:
Assuming the above schema is one set of a single field, it will be run against the document data, in which case it looks something like:
Feedback needed:
We would like to continue with this proposal and push said package, which we can make npm only like DataForms and DataViews.
Unlike #65171, this system is neither a DSL that we end up needing to maintain, nor to support a niche case that's yet to fully arise. Form validation is a current problem that we have right now, it would be great to solve.
The next are:
cc @youknowriad @oandregal
Beta Was this translation helpful? Give feedback.
All reactions