Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEVXP-2603: chore(next): use json schema test suite #121

Merged
merged 5 commits into from
Feb 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "next/json-schema-test-suite"]
lukad marked this conversation as resolved.
Show resolved Hide resolved
path = next/json-schema-test-suite
url = [email protected]:json-schema-org/JSON-Schema-Test-Suite.git
1 change: 1 addition & 0 deletions next/json-schema-test-suite
Submodule json-schema-test-suite added at e52450
51 changes: 51 additions & 0 deletions next/test/validation/json_schema_test_suite.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import type { JsfSchema, SchemaValue } from '../../src/types'
import fs from 'node:fs'
import path from 'node:path'
import util from 'node:util'
import { describe, expect, it } from '@jest/globals'
import { createHeadlessForm } from '../../src'

interface Test {
description: string
data: SchemaValue
valid: boolean
}

interface TestSchema {
description: string
schema: JsfSchema
tests: Test[]
}

expect.extend({
lukad marked this conversation as resolved.
Show resolved Hide resolved
toBeValid(received: JsfSchema, value: SchemaValue, valid: boolean = true) {
lukad marked this conversation as resolved.
Show resolved Hide resolved
const form = createHeadlessForm(received, { initialValues: value })
const validationResult = form.handleValidation(value)
const hasFormErrors = validationResult.formErrors !== undefined
const pass = hasFormErrors !== valid
return {
pass,
message: () => `expected ${util.inspect(value)} ${valid ? 'to' : 'not to'} be valid for ${util.inspect(received)}`,
}
},
})

describe.skip('JSON Schema Test Suite', () => {
const testsDir = path.join(__dirname, '..', '..', 'json-schema-test-suite', 'tests', 'draft2020-12')
const testFiles = fs.readdirSync(testsDir).filter(file => file.endsWith('.json'))
lukad marked this conversation as resolved.
Show resolved Hide resolved

for (const file of testFiles) {
const testFile: TestSchema[] = JSON.parse(fs.readFileSync(path.join(testsDir, file), 'utf8'))

for (const testSchema of testFile) {
describe(testSchema.description, () => {
for (const test of testSchema.tests) {
it(test.description, () => {
// TODO: properly extend the expect interface
expect(testSchema.schema).toBeValid(test.data, test.valid)
lukad marked this conversation as resolved.
Show resolved Hide resolved
})
}
})
}
}
})