@elementor/editor-props 3.35.0-340 → 3.35.0-341

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,3 @@
1
+ import { type PropValue, type TransformablePropValue } from '../../types';
2
+
3
+ export type PropValueTransformer = < T = PropValue | TransformablePropValue< string > >( value: T ) => T;
@@ -0,0 +1,150 @@
1
+ import { validate, type ValidationError } from 'jsonschema';
2
+
3
+ import { type PropType } from '../types';
4
+ import { propTypeToJsonSchema } from './props-to-llm-schema';
5
+
6
+ /**
7
+ * Detailed error information with nested anyOf variant errors
8
+ */
9
+ export interface DetailedValidationError {
10
+ path: ( string | number )[];
11
+ message: string;
12
+ schema?: unknown;
13
+ instance?: unknown;
14
+ name: string;
15
+ /**
16
+ * If this was an anyOf failure, contains errors for each variant
17
+ */
18
+ variants?: {
19
+ discriminator: string;
20
+ errors: DetailedValidationError[];
21
+ }[];
22
+ }
23
+
24
+ /**
25
+ * Recursively processes validation errors to provide detailed information about anyOf failures
26
+ *
27
+ * @param error The validation error to process
28
+ */
29
+ function processValidationError( error: ValidationError ): DetailedValidationError {
30
+ const detailed: DetailedValidationError = {
31
+ path: error.path,
32
+ message: error.message,
33
+ schema: error.schema,
34
+ instance: error.instance,
35
+ name: error.name,
36
+ };
37
+
38
+ // If this is an anyOf error, re-validate against each variant to get nested errors
39
+ if ( error.name === 'anyOf' && error.schema && typeof error.schema === 'object' && 'anyOf' in error.schema ) {
40
+ const anyOfSchema = error.schema as { anyOf?: unknown[] };
41
+ const variants = ( anyOfSchema.anyOf || [] ).map( ( variantSchema, idx ) => {
42
+ // Re-validate the instance against this specific variant
43
+ const variantResult = validate( error.instance, variantSchema );
44
+
45
+ // Get discriminator from schema if available
46
+ let discriminator = `variant-${ idx }`;
47
+ if (
48
+ variantSchema &&
49
+ typeof variantSchema === 'object' &&
50
+ 'properties' in variantSchema &&
51
+ variantSchema.properties &&
52
+ typeof variantSchema.properties === 'object' &&
53
+ '$$type' in variantSchema.properties
54
+ ) {
55
+ const typeProperty = variantSchema.properties.$$type;
56
+ if (
57
+ typeProperty &&
58
+ typeof typeProperty === 'object' &&
59
+ 'const' in typeProperty &&
60
+ typeof typeProperty.const === 'string'
61
+ ) {
62
+ discriminator = typeProperty.const;
63
+ }
64
+ }
65
+
66
+ return {
67
+ discriminator,
68
+ errors: variantResult.errors.map( processValidationError ),
69
+ };
70
+ } );
71
+
72
+ detailed.variants = variants;
73
+ }
74
+
75
+ return detailed;
76
+ }
77
+
78
+ /**
79
+ * Formats detailed errors into a human-readable string
80
+ * @param errors
81
+ * @param indent
82
+ */
83
+ function formatDetailedErrors( errors: DetailedValidationError[], indent = '' ): string {
84
+ const lines: string[] = [];
85
+
86
+ for ( const error of errors ) {
87
+ const pathStr = error.path.length > 0 ? error.path.join( '.' ) : 'root';
88
+ lines.push( `${ indent }Error at ${ pathStr }: ${ error.message }` );
89
+
90
+ if ( error.variants && error.variants.length > 0 ) {
91
+ lines.push( `${ indent } Tried ${ error.variants.length } variant(s):` );
92
+ for ( const variant of error.variants ) {
93
+ lines.push( `${ indent } - ${ variant.discriminator }:` );
94
+ if ( variant.errors.length === 0 ) {
95
+ lines.push( `${ indent } (no errors - this variant matched!)` );
96
+ } else {
97
+ for ( const nestedError of variant.errors ) {
98
+ const nestedPathStr = nestedError.path.length > 0 ? nestedError.path.join( '.' ) : 'root';
99
+ lines.push( `${ indent } ${ nestedPathStr }: ${ nestedError.message }` );
100
+
101
+ // Recursively format nested variant errors
102
+ if ( nestedError.variants && nestedError.variants.length > 0 ) {
103
+ lines.push( formatDetailedErrors( [ nestedError ], `${ indent } ` ) );
104
+ }
105
+ }
106
+ }
107
+ }
108
+ }
109
+ }
110
+
111
+ return lines.join( '\n' );
112
+ }
113
+
114
+ export const validatePropValue = ( schema: PropType, value: unknown ) => {
115
+ const jsonSchema = propTypeToJsonSchema( schema );
116
+ const result = validate( value, jsonSchema );
117
+ const detailedErrors = result.errors.map( processValidationError );
118
+ return {
119
+ valid: result.valid,
120
+ errors: result.errors,
121
+ errorMessages: formatDetailedErrors( detailedErrors ),
122
+ jsonSchema: JSON.stringify( jsonSchema ),
123
+ };
124
+ };
125
+
126
+ /**
127
+ * Validates a prop value with detailed error reporting for anyOf failures
128
+ *
129
+ * This function provides enhanced error messages that show exactly which nested
130
+ * properties failed validation in anyOf schemas, making debugging much easier.
131
+ *
132
+ * @param schema The PropType schema to validate against
133
+ * @param value The value to validate
134
+ * @return Validation result with detailed error information
135
+ */
136
+ export const validatePropValueDetailed = ( schema: PropType, value: unknown ) => {
137
+ const jsonSchema = propTypeToJsonSchema( schema );
138
+ const result = validate( value, jsonSchema );
139
+
140
+ // Process all errors to add detailed anyOf information
141
+ const detailedErrors = result.errors.map( processValidationError );
142
+
143
+ return {
144
+ valid: result.valid,
145
+ errors: detailedErrors,
146
+ errorMessages: detailedErrors.map( ( err ) => err.message ),
147
+ formattedErrors: formatDetailedErrors( detailedErrors ),
148
+ jsonSchema: JSON.stringify( jsonSchema ),
149
+ };
150
+ };