@interweave/interweave
Advanced tools
Comparing version 0.0.26 to 0.0.27
export declare const isEmpty: (value: any) => boolean; | ||
export declare function get(object: object, path: string, defaultValue?: null): any; | ||
export declare const isValuePresent: (fullObject: object, target: string) => boolean; | ||
export declare function arraysAreEqual(a: any[], b: any[]): boolean; |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isValuePresent = exports.get = exports.isEmpty = void 0; | ||
exports.arraysAreEqual = exports.isValuePresent = exports.get = exports.isEmpty = void 0; | ||
const is_empty_1 = __importDefault(require("is-empty")); | ||
@@ -39,1 +39,19 @@ const isEmpty = (value) => { | ||
exports.isValuePresent = isValuePresent; | ||
function arraysAreEqual(a, b) { | ||
if (a === b) | ||
return true; | ||
if (a == null || b == null) | ||
return false; | ||
if (a.length !== b.length) | ||
return false; | ||
// If you don't care about the order of the elements inside | ||
// the array, you should sort both arrays here. | ||
// Please note that calling sort on an array will modify that array. | ||
// you might want to clone your array first. | ||
for (var i = 0; i < a.length; ++i) { | ||
if (a[i] !== b[i]) | ||
return false; | ||
} | ||
return true; | ||
} | ||
exports.arraysAreEqual = arraysAreEqual; |
@@ -90,2 +90,24 @@ export interface Error { | ||
ensure_empty_if_none_empty?: string[]; | ||
/** | ||
* Make sure this key is present if some other key equals X value | ||
* If key is an array value and array is supplied, it'll check equality of the arrays | ||
* If key is a non-array value and array is supplied, it'll check each of the values in the array | ||
* If key is an array value and a non-array value is supplied, invalid configuration | ||
* If key is a non-array value and a non-array is supplied, it'll check type and value equality | ||
*/ | ||
ensure_present_if_key_equals?: { | ||
key: string; | ||
value?: string | number | boolean | string[] | number[]; | ||
}; | ||
/** | ||
* Make sure this key is empty if some other key equals X value | ||
* If key is an array value and array is supplied, it'll check equality of the arrays | ||
* If key is a non-array value and array is supplied, it'll check each of the values in the array | ||
* If key is an array value and a non-array value is supplied, invalid configuration | ||
* If key is a non-array value and a non-array is supplied, it'll check type and value equality | ||
*/ | ||
ensure_empty_if_key_equals?: { | ||
key: string; | ||
value: string | number | boolean | string[] | number[]; | ||
}; | ||
}; | ||
@@ -92,0 +114,0 @@ /** |
@@ -272,4 +272,4 @@ "use strict"; | ||
__bar: "<parameters.bar_id>", | ||
title: "Trivia", | ||
description: "this is some trivia", | ||
title: ["bla"], | ||
description: "", | ||
sport: null, | ||
@@ -421,2 +421,8 @@ featured_image: null, | ||
}, | ||
validation: { | ||
ensure_present_if_key_equals: { | ||
key: "title", | ||
value: ["bla", "blaa"], | ||
}, | ||
}, | ||
}, special_event: { | ||
@@ -423,0 +429,0 @@ schema: { |
@@ -5,2 +5,3 @@ "use strict"; | ||
const helpers_1 = require("./helpers"); | ||
const helpers_2 = require("./helpers"); | ||
const throwError = (err) => { | ||
@@ -190,2 +191,9 @@ throw new Error(`${err}`); | ||
} | ||
// Make sure value is an array if it was specified | ||
const expectsArray = config.schema.is_array; | ||
if (expectsArray) { | ||
if (!Array.isArray(value)) { | ||
error(`Key '${key}' was specified as an array field, but an array was not received.`); | ||
} | ||
} | ||
if (config.validation) { | ||
@@ -247,2 +255,76 @@ // Lets do our ensure present checks before possible returning on null | undefined | ||
} | ||
// Make sure a key is present if some other key equals X value | ||
if (config.validation.ensure_present_if_key_equals) { | ||
const target = config.validation.ensure_present_if_key_equals; | ||
const targetKey = target.key; | ||
const targetValue = target.value; | ||
const actualValue = (0, helpers_2.get)(fullValueObject, targetKey); | ||
let conditionTrue = actualValue === targetValue; | ||
const isActualArray = Array.isArray(actualValue); | ||
const isTargetArray = Array.isArray(targetValue); | ||
// Do a deep equality to make sure arrays are equal | ||
if (isActualArray && isTargetArray) { | ||
if ((0, helpers_1.arraysAreEqual)(targetValue, actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
// This shouldn't happen and should get caught in the schema validator first | ||
if (isActualArray && !isTargetArray) { | ||
error(`Invalid key configuration for key ${key} in field 'ensure_present_if_key_equals'. The returned`); | ||
// conditionTrue = true; | ||
} | ||
if (!isActualArray && isTargetArray) { | ||
// @ts-expect-error | ||
if (targetValue.includes(actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (!isActualArray && !isTargetArray) { | ||
if (targetValue === actualValue) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (conditionTrue) { | ||
if ((0, helpers_1.isEmpty)(value)) { | ||
error(`${key} must have a value when ${targetKey} has a value of ${targetValue}`); | ||
} | ||
} | ||
} | ||
// Make sure a key is present if some other key equals X value | ||
if (config.validation.ensure_empty_if_key_equals) { | ||
const target = config.validation.ensure_empty_if_key_equals; | ||
const targetKey = target.key; | ||
const targetValue = target.value; | ||
const actualValue = (0, helpers_2.get)(fullValueObject, targetKey); | ||
let conditionTrue = actualValue === targetValue; | ||
const isActualArray = Array.isArray(actualValue); | ||
const isTargetArray = Array.isArray(targetValue); | ||
// Do a deep equality to make sure arrays are equal | ||
if (isActualArray && isTargetArray) { | ||
if ((0, helpers_1.arraysAreEqual)(targetValue, actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
// This shouldn't happen and should get caught in the schema validator first | ||
if (isActualArray && !isTargetArray) { | ||
error(`Invalid key configuration for key ${key} in field 'ensure_present_if_key_equals'. The returned`); | ||
// conditionTrue = true; | ||
} | ||
if (!isActualArray && isTargetArray) { | ||
// @ts-expect-error | ||
if (targetValue.includes(actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (!isActualArray && !isTargetArray) { | ||
if (targetValue === actualValue) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (conditionTrue) { | ||
if (!(0, helpers_1.isEmpty)(value)) { | ||
error(`${key} must have a value when ${targetKey} has a value of ${targetValue}`); | ||
} | ||
} | ||
} | ||
} | ||
@@ -253,9 +335,2 @@ // If it's optional and not present, lets move on | ||
} | ||
// Make sure value is an array if it was specified | ||
const expectsArray = config.schema.is_array; | ||
if (expectsArray) { | ||
if (!Array.isArray(value)) { | ||
error(`Key '${key}' was specified as an array field, but no array was received.`); | ||
} | ||
} | ||
// Make sure value objects are configured correctly | ||
@@ -262,0 +337,0 @@ const validArrayType = expectsArray && Array.isArray(value); |
{ | ||
"name": "@interweave/interweave", | ||
"version": "0.0.26", | ||
"version": "0.0.27", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -36,1 +36,17 @@ import empty from "is-empty"; | ||
}; | ||
export function arraysAreEqual(a: any[], b: any[]) { | ||
if (a === b) return true; | ||
if (a == null || b == null) return false; | ||
if (a.length !== b.length) return false; | ||
// If you don't care about the order of the elements inside | ||
// the array, you should sort both arrays here. | ||
// Please note that calling sort on an array will modify that array. | ||
// you might want to clone your array first. | ||
for (var i = 0; i < a.length; ++i) { | ||
if (a[i] !== b[i]) return false; | ||
} | ||
return true; | ||
} |
@@ -113,2 +113,24 @@ export interface Error { | ||
ensure_empty_if_none_empty?: string[]; | ||
/** | ||
* Make sure this key is present if some other key equals X value | ||
* If key is an array value and array is supplied, it'll check equality of the arrays | ||
* If key is a non-array value and array is supplied, it'll check each of the values in the array | ||
* If key is an array value and a non-array value is supplied, invalid configuration | ||
* If key is a non-array value and a non-array is supplied, it'll check type and value equality | ||
*/ | ||
ensure_present_if_key_equals?: { | ||
key: string; | ||
value?: string | number | boolean | string[] | number[]; | ||
}; | ||
/** | ||
* Make sure this key is empty if some other key equals X value | ||
* If key is an array value and array is supplied, it'll check equality of the arrays | ||
* If key is a non-array value and array is supplied, it'll check each of the values in the array | ||
* If key is an array value and a non-array value is supplied, invalid configuration | ||
* If key is a non-array value and a non-array is supplied, it'll check type and value equality | ||
*/ | ||
ensure_empty_if_key_equals?: { | ||
key: string; | ||
value: string | number | boolean | string[] | number[]; | ||
}; | ||
}; | ||
@@ -115,0 +137,0 @@ /** |
@@ -43,2 +43,29 @@ import { test, expect, describe } from "@jest/globals"; | ||
}); | ||
// describe("ensure_present_if_key_equals", () => { | ||
// test("value is array & target is array", () => { | ||
// const newSchema = structuredClone(schema); | ||
// newSchema.keys.first_name.validation!.ensure_empty_if_none_empty = | ||
// ["string"]; | ||
// expect(() => validateSchema(newSchema)).toThrowError(); | ||
// }); | ||
// test("value is string & target is string", () => { | ||
// const newSchema = structuredClone(schema); | ||
// newSchema.keys.first_name.validation!.ensure_empty_if_none_empty = | ||
// ["string"]; | ||
// expect(() => validateSchema(newSchema)).toThrowError(); | ||
// }); | ||
// test("value is string & target is array", () => { | ||
// const newSchema = structuredClone(schema); | ||
// newSchema.keys.first_name.validation!.ensure_empty_if_none_empty = | ||
// ["string"]; | ||
// expect(() => validateSchema(newSchema)).toThrowError(); | ||
// }); | ||
// test("value is array & target is array", () => { | ||
// // Should be invalid | ||
// const newSchema = structuredClone(schema); | ||
// newSchema.keys.first_name.validation!.ensure_empty_if_none_empty = | ||
// ["string"]; | ||
// expect(() => validateSchema(newSchema)).toThrowError(); | ||
// }); | ||
// }); | ||
}); | ||
@@ -45,0 +72,0 @@ describe("validate string operations", () => { |
@@ -1,3 +0,4 @@ | ||
import { isEmpty, isValuePresent } from "./helpers"; | ||
import { arraysAreEqual, isEmpty, isValuePresent } from "./helpers"; | ||
import { type Schema, type KeyConfiguration } from "./interfaces"; | ||
import { get } from "./helpers"; | ||
@@ -266,2 +267,12 @@ const throwError = (err: string) => { | ||
// Make sure value is an array if it was specified | ||
const expectsArray = config.schema.is_array; | ||
if (expectsArray) { | ||
if (!Array.isArray(value)) { | ||
error( | ||
`Key '${key}' was specified as an array field, but an array was not received.` | ||
); | ||
} | ||
} | ||
if (config.validation) { | ||
@@ -348,2 +359,98 @@ // Lets do our ensure present checks before possible returning on null | undefined | ||
} | ||
// Make sure a key is present if some other key equals X value | ||
if (config.validation.ensure_present_if_key_equals) { | ||
const target = config.validation.ensure_present_if_key_equals; | ||
const targetKey = target.key; | ||
const targetValue = target.value; | ||
const actualValue = get(fullValueObject, targetKey); | ||
let conditionTrue = actualValue === targetValue; | ||
const isActualArray = Array.isArray(actualValue); | ||
const isTargetArray = Array.isArray(targetValue); | ||
// Do a deep equality to make sure arrays are equal | ||
if (isActualArray && isTargetArray) { | ||
if (arraysAreEqual(targetValue, actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
// This shouldn't happen and should get caught in the schema validator first | ||
if (isActualArray && !isTargetArray) { | ||
error( | ||
`Invalid key configuration for key ${key} in field 'ensure_present_if_key_equals'. The returned` | ||
); | ||
// conditionTrue = true; | ||
} | ||
if (!isActualArray && isTargetArray) { | ||
// @ts-expect-error | ||
if (targetValue.includes(actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (!isActualArray && !isTargetArray) { | ||
if (targetValue === actualValue) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (conditionTrue) { | ||
if (isEmpty(value)) { | ||
error( | ||
`${key} must have a value when ${targetKey} has a value of ${targetValue}` | ||
); | ||
} | ||
} | ||
} | ||
// Make sure a key is present if some other key equals X value | ||
if (config.validation.ensure_empty_if_key_equals) { | ||
const target = config.validation.ensure_empty_if_key_equals; | ||
const targetKey = target.key; | ||
const targetValue = target.value; | ||
const actualValue = get(fullValueObject, targetKey); | ||
let conditionTrue = actualValue === targetValue; | ||
const isActualArray = Array.isArray(actualValue); | ||
const isTargetArray = Array.isArray(targetValue); | ||
// Do a deep equality to make sure arrays are equal | ||
if (isActualArray && isTargetArray) { | ||
if (arraysAreEqual(targetValue, actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
// This shouldn't happen and should get caught in the schema validator first | ||
if (isActualArray && !isTargetArray) { | ||
error( | ||
`Invalid key configuration for key ${key} in field 'ensure_present_if_key_equals'. The returned` | ||
); | ||
// conditionTrue = true; | ||
} | ||
if (!isActualArray && isTargetArray) { | ||
// @ts-expect-error | ||
if (targetValue.includes(actualValue)) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (!isActualArray && !isTargetArray) { | ||
if (targetValue === actualValue) { | ||
conditionTrue = true; | ||
} | ||
} | ||
if (conditionTrue) { | ||
if (!isEmpty(value)) { | ||
error( | ||
`${key} must have a value when ${targetKey} has a value of ${targetValue}` | ||
); | ||
} | ||
} | ||
} | ||
} | ||
@@ -356,12 +463,2 @@ | ||
// Make sure value is an array if it was specified | ||
const expectsArray = config.schema.is_array; | ||
if (expectsArray) { | ||
if (!Array.isArray(value)) { | ||
error( | ||
`Key '${key}' was specified as an array field, but no array was received.` | ||
); | ||
} | ||
} | ||
// Make sure value objects are configured correctly | ||
@@ -368,0 +465,0 @@ const validArrayType = expectsArray && Array.isArray(value); |
124559
3549