11import type { ValidationError , ValidationErrorPath } from '../errors'
2- import type { JsfSchema , NonBooleanJsfSchema , SchemaValue } from '../types'
2+ import type { JsfSchema , JsonLogicBag , NonBooleanJsfSchema , SchemaValue } from '../types'
33import { validateSchema , type ValidationOptions } from './schema'
44import { deepEqual } from './util'
55
@@ -8,6 +8,7 @@ import { deepEqual } from './util'
88 * @param value - The value to validate
99 * @param schema - The schema to validate against
1010 * @param options - The validation options
11+ * @param jsonLogicBag - The JSON logic bag
1112 * @param path - The path to the current field being validated
1213 * @returns An array of validation errors
1314 * @description
@@ -18,6 +19,7 @@ export function validateArray(
1819 value : SchemaValue ,
1920 schema : JsfSchema ,
2021 options : ValidationOptions ,
22+ jsonLogicBag : JsonLogicBag | undefined ,
2123 path : ValidationErrorPath ,
2224) : ValidationError [ ] {
2325 if ( ! Array . isArray ( value ) ) {
@@ -27,9 +29,9 @@ export function validateArray(
2729 return [
2830 ...validateLength ( schema , value , path ) ,
2931 ...validateUniqueItems ( schema , value , path ) ,
30- ...validateContains ( value , schema , options , path ) ,
31- ...validatePrefixItems ( schema , value , options , path ) ,
32- ...validateItems ( schema , value , options , path ) ,
32+ ...validateContains ( value , schema , options , jsonLogicBag , path ) ,
33+ ...validatePrefixItems ( schema , value , options , jsonLogicBag , path ) ,
34+ ...validateItems ( schema , value , options , jsonLogicBag , path ) ,
3335 ]
3436}
3537
@@ -44,7 +46,11 @@ export function validateArray(
4446 * If the `maxItems` keyword is defined, the array must contain at most `maxItems` items.
4547 * If the `minItems` keyword is defined, the array must contain at least `minItems` items.
4648 */
47- function validateLength ( schema : NonBooleanJsfSchema , value : SchemaValue [ ] , path : ValidationErrorPath ) : ValidationError [ ] {
49+ function validateLength (
50+ schema : NonBooleanJsfSchema ,
51+ value : SchemaValue [ ] ,
52+ path : ValidationErrorPath ,
53+ ) : ValidationError [ ] {
4854 const errors : ValidationError [ ] = [ ]
4955
5056 const itemsLength = value . length
@@ -65,14 +71,21 @@ function validateLength(schema: NonBooleanJsfSchema, value: SchemaValue[], path:
6571 * @param schema - The schema to validate against
6672 * @param values - The array value to validate
6773 * @param options - The validation options
74+ * @param jsonLogicBag - The JSON logic bag
6875 * @param path - The path to the current field being validated
6976 * @returns An array of validation errors
7077 * @description
7178 * Validates the items constraint of an array.
7279 * If the `items` keyword is defined, each item in the array must match the schema of the `items` keyword.
7380 * When the `prefixItems` keyword is defined, the items constraint is validated only for the items after the prefix items.
7481 */
75- function validateItems ( schema : NonBooleanJsfSchema , values : SchemaValue [ ] , options : ValidationOptions , path : ValidationErrorPath ) : ValidationError [ ] {
82+ function validateItems (
83+ schema : NonBooleanJsfSchema ,
84+ values : SchemaValue [ ] ,
85+ options : ValidationOptions ,
86+ jsonLogicBag : JsonLogicBag | undefined ,
87+ path : ValidationErrorPath ,
88+ ) : ValidationError [ ] {
7689 if ( schema . items === undefined ) {
7790 return [ ]
7891 }
@@ -81,7 +94,15 @@ function validateItems(schema: NonBooleanJsfSchema, values: SchemaValue[], optio
8194 const startIndex = Array . isArray ( schema . prefixItems ) ? schema . prefixItems . length : 0
8295
8396 for ( const [ i , item ] of values . slice ( startIndex ) . entries ( ) ) {
84- errors . push ( ...validateSchema ( item , schema . items , options , [ ...path , 'items' , i + startIndex ] ) )
97+ errors . push (
98+ ...validateSchema (
99+ item ,
100+ schema . items ,
101+ options ,
102+ [ ...path , 'items' , i + startIndex ] ,
103+ jsonLogicBag ,
104+ ) ,
105+ )
85106 }
86107
87108 return errors
@@ -92,21 +113,36 @@ function validateItems(schema: NonBooleanJsfSchema, values: SchemaValue[], optio
92113 * @param schema - The schema to validate against
93114 * @param values - The array value to validate
94115 * @param options - The validation options
116+ * @param jsonLogicBag - The JSON logic bag
95117 * @param path - The path to the current field being validated
96118 * @returns An array of validation errors
97119 * @description
98120 * Validates the prefixItems constraint of an array.
99121 * If the `prefixItems` keyword is defined, each item in the array must match the schema of the corresponding prefix item.
100122 */
101- function validatePrefixItems ( schema : NonBooleanJsfSchema , values : SchemaValue [ ] , options : ValidationOptions , path : ValidationErrorPath ) : ValidationError [ ] {
123+ function validatePrefixItems (
124+ schema : NonBooleanJsfSchema ,
125+ values : SchemaValue [ ] ,
126+ options : ValidationOptions ,
127+ jsonLogicBag : JsonLogicBag | undefined ,
128+ path : ValidationErrorPath ,
129+ ) : ValidationError [ ] {
102130 if ( ! Array . isArray ( schema . prefixItems ) ) {
103131 return [ ]
104132 }
105133
106134 const errors : ValidationError [ ] = [ ]
107135 for ( const [ i , item ] of values . entries ( ) ) {
108136 if ( i < schema . prefixItems . length ) {
109- errors . push ( ...validateSchema ( item , schema . prefixItems [ i ] as JsfSchema , options , [ ...path , 'prefixItems' , i ] ) )
137+ errors . push (
138+ ...validateSchema (
139+ item ,
140+ schema . prefixItems [ i ] as JsfSchema ,
141+ options ,
142+ [ ...path , 'prefixItems' , i ] ,
143+ jsonLogicBag ,
144+ ) ,
145+ )
110146 }
111147 }
112148
@@ -118,6 +154,7 @@ function validatePrefixItems(schema: NonBooleanJsfSchema, values: SchemaValue[],
118154 * @param value - The array value to validate
119155 * @param schema - The schema to validate against
120156 * @param options - The validation options
157+ * @param jsonLogicBag - The JSON logic bag
121158 * @param path - The path to the current field being validated
122159 * @returns An array of validation errors
123160 * @description
@@ -128,6 +165,7 @@ function validateContains(
128165 value : SchemaValue [ ] ,
129166 schema : NonBooleanJsfSchema ,
130167 options : ValidationOptions ,
168+ jsonLogicBag : JsonLogicBag | undefined ,
131169 path : ValidationErrorPath ,
132170) : ValidationError [ ] {
133171 if ( ! ( 'contains' in schema ) ) {
@@ -137,8 +175,15 @@ function validateContains(
137175 const errors : ValidationError [ ] = [ ]
138176
139177 // How many items in the array are valid against the contains schema?
140- const contains = value . filter ( item =>
141- validateSchema ( item , schema . contains as JsfSchema , options , [ ...path , 'contains' ] ) . length === 0 ,
178+ const contains = value . filter (
179+ item =>
180+ validateSchema (
181+ item ,
182+ schema . contains as JsfSchema ,
183+ options ,
184+ [ ...path , 'contains' ] ,
185+ jsonLogicBag ,
186+ ) . length === 0 ,
142187 ) . length
143188
144189 if ( schema . minContains === undefined && schema . maxContains === undefined ) {
@@ -168,7 +213,11 @@ function validateContains(
168213 * @description
169214 * Validates the uniqueItems constraint of an array when the `uniqueItems` keyword is defined as `true`.
170215 */
171- function validateUniqueItems ( schema : NonBooleanJsfSchema , values : SchemaValue [ ] , path : ValidationErrorPath ) : ValidationError [ ] {
216+ function validateUniqueItems (
217+ schema : NonBooleanJsfSchema ,
218+ values : SchemaValue [ ] ,
219+ path : ValidationErrorPath ,
220+ ) : ValidationError [ ] {
172221 if ( schema . uniqueItems !== true ) {
173222 return [ ]
174223 }
0 commit comments