Skip to content

Commit

Permalink
DEVXP-2603: chore(next): use json schema test suite (#121)
Browse files Browse the repository at this point in the history
Co-authored-by: Capelo <[email protected]>
  • Loading branch information
lukad and antoniocapelo authored Feb 6, 2025
1 parent 07c7ea1 commit 413b077
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
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"]
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({
toBeValid(received: JsfSchema, value: SchemaValue, valid: boolean = true) {
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'))

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)
})
}
})
}
}
})

0 comments on commit 413b077

Please sign in to comment.