summaryrefslogtreecommitdiff
path: root/test/json-schema.test.js
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-29 22:06:38 -0500
committertoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-11-29 22:06:38 -0500
commit01142ef3d62e388a1c952ad9ea3a01ed38b518b6 (patch)
tree0ab39a1457d6c0c13c61d19cec0c3766a9125079 /test/json-schema.test.js
parentf0c5ef69ca1758d5ef9201bc8a4375e5933888be (diff)
Update types
Diffstat (limited to 'test/json-schema.test.js')
-rw-r--r--test/json-schema.test.js71
1 files changed, 53 insertions, 18 deletions
diff --git a/test/json-schema.test.js b/test/json-schema.test.js
index 5370e8da..e534f538 100644
--- a/test/json-schema.test.js
+++ b/test/json-schema.test.js
@@ -19,25 +19,47 @@
import {expect, test} from 'vitest';
import {JsonSchema} from '../ext/js/data/json-schema.js';
+/**
+ * @param {import('json-schema').Schema} schema
+ * @param {unknown} value
+ * @returns {boolean}
+ */
function schemaValidate(schema, value) {
return new JsonSchema(schema).isValid(value);
}
+/**
+ * @param {import('json-schema').Schema} schema
+ * @param {unknown} value
+ * @returns {import('json-schema').Value}
+ */
function getValidValueOrDefault(schema, value) {
return new JsonSchema(schema).getValidValueOrDefault(value);
}
+/**
+ * @param {import('json-schema').Schema} schema
+ * @param {import('json-schema').Value} value
+ * @returns {import('json-schema').Value}
+ */
function createProxy(schema, value) {
return new JsonSchema(schema).createProxy(value);
}
+/**
+ * @template T
+ * @param {T} value
+ * @returns {T}
+ */
function clone(value) {
return JSON.parse(JSON.stringify(value));
}
+/** */
function testValidate1() {
test('Validate1', () => {
+ /** @type {import('json-schema').Schema} */
const schema = {
allOf: [
{
@@ -56,28 +78,34 @@ function testValidate1() {
]
},
{
- not: [
- {multipleOf: 20}
- ]
+ not: {
+ anyOf: [
+ {multipleOf: 20}
+ ]
+ }
}
]
};
+ /**
+ * @param {number} value
+ * @returns {boolean}
+ */
const jsValidate = (value) => {
return (
typeof value === 'number' &&
- (
- (value >= 10 && value <= 100) ||
- (value >= -100 && value <= -10)
- ) &&
- (
(
- (value % 3) === 0 ||
- (value % 5) === 0
+ (value >= 10 && value <= 100) ||
+ (value >= -100 && value <= -10)
+ ) &&
+ (
+ (
+ (value % 3) === 0 ||
+ (value % 5) === 0
+ ) &&
+ (value % 15) !== 0
) &&
- (value % 15) !== 0
- ) &&
- (value % 20) !== 0
+ (value % 20) !== 0
);
};
@@ -89,10 +117,12 @@ function testValidate1() {
});
}
+/** */
function testValidate2() {
test('Validate2', () => {
+ /** @type {{schema: import('json-schema').Schema, inputs: {expected: boolean, value: unknown}[]}[]} */
const data = [
- // String tests
+ // String tests
{
schema: {
type: 'string'
@@ -494,10 +524,12 @@ function testValidate2() {
}
+/** */
function testGetValidValueOrDefault1() {
test('GetValidValueOrDefault1', () => {
+ /** @type {{schema: import('json-schema').Schema, inputs: [value: unknown, expected: unknown][]}[]} */
const data = [
- // Test value defaulting on objects with additionalProperties=false
+ // Test value defaulting on objects with additionalProperties=false
{
schema: {
type: 'object',
@@ -667,10 +699,10 @@ function testGetValidValueOrDefault1() {
type: 'object',
required: ['toString'],
properties: {
- toString: {
+ toString: /** @type {import('json-schema').SchemaObject} */ ({
type: 'string',
default: 'default'
- }
+ })
}
},
inputs: [
@@ -850,10 +882,12 @@ function testGetValidValueOrDefault1() {
}
+/** */
function testProxy1() {
test('Proxy1', () => {
+ /** @type {{schema: import('json-schema').Schema, tests: {error: boolean, value?: import('json-schema').Value, action: (value: import('core').SafeAny) => void}[]}[]} */
const data = [
- // Object tests
+ // Object tests
{
schema: {
type: 'object',
@@ -998,6 +1032,7 @@ function testProxy1() {
}
+/** */
function main() {
testValidate1();
testValidate2();