Schema Validation & Schema Generation
Restlyn supports optional contract testing via JSON Schema validation. It also automatically generate JSON schema from live responses.
π Schema Validation...How It Worksβ
When a Gherkin step includes:
Then response should match schema "userSchema"
Restlyn will:
- Load the
userSchema.schema.jsonfrom yourschemas/folder - Validate the response using AJV
- Fail the test if the schema doesnβt match
π Default Schema Locationβ
All schemas are expected under:
schemas/
You can override this using:
restlyn tests --schemas ./custom-folder
π§Ύ Schema Validation Errorsβ
If validation fails during a test:
- β Missing fields are listed
- β οΈ Extra/unexpected fields are shown
- Detailed AJV error messages are printed
𧬠Generate a Schema Automaticallyβ
Use the CLI command:
restlyn schema --url /profile --method GET --output userProfile --env local
restlyn schema --url /profile --method GET --output userProfile --env local \
--headers '{"Authorization":"Bearer mocked-token-123"}'
Optional flags:
--method,--headers,--body,--env
Restlyn will:
-
Fetch the live API response
-
Convert it to a basic JSON schema
-
Save to
schemas/userProfile.schema.json -
Small notes:
By default, schemas are looked up under paths.schemas in your .restlynrc (falls back to ./schemas). The restlyn tests command also lets you override with --schemas
dir.The schema CLI builds the full URL from .restlynrcβs baseUrls.local or baseUrls.prod depending on --env. If you pass an absolute --url, it will use it as-is.
π Strict vs Lenient (Upcoming)β
Future versions will support --strict flag to:
- Fail on extra fields (currently allowed)
- Enforce exact schema match
Use schemas to ensure your API contracts donβt silently break!
β‘οΈ Next: Data-Driven Testing