Skip to content

Conversation

@priethor
Copy link
Contributor

@priethor priethor commented Oct 20, 2025

What

Part of #162.

Adds JSON schema validation infrastructure for post type definitions. This not only improves the creation of new Post Types via JSON files, but also makes integrating the Abilities API easier.

Why

SCF supports exporting and importing its entities through JSON files, but the lack of schemas makes it difficult to create them from scratch; even LLMs have trouble creating a basic, working JSON file for post types, as seen in the included fixture of a wrong format.

Having these schemas will help create new post types from scratch by:

  • Clearly specifying what attibutes are accepted, required, etc.
  • Validating post type configurations before registration to prevent runtime errors
  • Providing clear validation feedback for invalid configurations, like when trying to use a WordPress reserved term
  • Enables better tooling support with schema definitions

How

  • Validator Class: New SCF_JSON_Schema_Validator that accepts different format (JSON files, strings, or PHP objects) and serves as a base for post type schemas and future ones.
  • Schema Files: Comprehensive JSON schema for post types with WordPress and SCF-specific validation rules
  • Common Definitions: Shared schema components including WordPress reserved terms, which willl come in handy for the other entities.
  • Test Coverage:added multiple tests to validate both positive and negative cases, including:
    • A real SCF export
    • An invalid file generated by LLMs to prove why the schemas are important
  • Export Format Support: SCF currently exports some things a bit raw, like booleans being 1. The import and schema accept both strings and boolean values. The same goes with single/multiple post types, the import accepts both.

Testing Instructions

  1. Run composer test:php tests/php/PostTypeSchemaTest.php --verbose - all post type schema validation tests should pass
  2. Verify schema validation works with both valid and invalid post type configurations adding them to the fixtures folder
  3. Most importantly, test with exported post type JSON files to ensure import format compatibility:
    3.1 Create a post type
    3.2 Go to SCF -> Tools, export the post type
    3.3 Put it in tests/php/fixtures/schemas/post-types/valid
    3.4 Run again the test via composer test:php tests/php/PostTypeSchemaTest.php --verbose, it should pass.

@priethor priethor self-assigned this Oct 20, 2025
},
"description": "The post type key passed to register_post_type() (e.g. 'book', 'product'). Max 20 characters, lowercase letters/numbers/underscores/dashes only, cannot be WordPress reserved terms."
},
"ID": {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I exported a json post type and could not see any ID, I guess it will be internal.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is internal, but upon a more detailed check, I've noticed it's actually always ignored in favor of the key property, so:

  • When importing a new Post Type, the ID is not used and is assigned upon DB insertion.
  • When importing an existing Post Type, the ID is overwritten with the actual ID.

All in all, it's only metadata that represents the Post Type, but since it's never used during import or export, let's remove it.

Good catch! ☺️

Copy link
Contributor

@cbravobernal cbravobernal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a long PR, I couldn't find anything weird or out of a default export.
I guess we may find some issues in a future when is used, but we need that testing / feedback.

@priethor priethor merged commit 440d7e1 into trunk Oct 31, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants