@@ -18,7 +18,7 @@ export const SCHEMA_VARIANTS = {
1818} ;
1919
2020class Validator {
21- constructor ( { Ajv, ajvErrors, schemaName} ) {
21+ constructor ( { Ajv, ajvErrors, schemaName, ajvConfig } ) {
2222 if ( ! schemaName || ! SCHEMA_VARIANTS [ schemaName ] ) {
2323 throw new Error (
2424 `"schemaName" is missing or incorrect. The available schemaName variants are ${ Object . keys (
@@ -29,11 +29,12 @@ class Validator {
2929
3030 this . _schemaName = SCHEMA_VARIANTS [ schemaName ] ;
3131
32- this . ajv = new Ajv ( {
32+ ajvConfig = Object . assign ( {
3333 allErrors : true ,
3434 jsonPointers : true ,
3535 loadSchema : Validator . loadSchema
36- } ) ;
36+ } , ajvConfig ) ;
37+ this . ajv = new Ajv ( ajvConfig ) ;
3738 ajvErrors ( this . ajv ) ;
3839 }
3940
@@ -77,6 +78,7 @@ class Validator {
7778}
7879
7980const validator = Object . create ( null ) ;
81+ const defaultsValidator = Object . create ( null ) ;
8082
8183async function _validate ( schemaName , options ) {
8284 if ( ! validator [ schemaName ] ) {
@@ -91,6 +93,27 @@ async function _validate(schemaName, options) {
9193 await schemaValidator . validate ( options ) ;
9294}
9395
96+ async function _validateAndSetDefaults ( schemaName , options ) {
97+ if ( ! defaultsValidator [ schemaName ] ) {
98+ defaultsValidator [ schemaName ] = ( async ( ) => {
99+ const { default : Ajv } = await import ( "ajv" ) ;
100+ const { default : ajvErrors } = await import ( "ajv-errors" ) ;
101+ return new Validator ( { Ajv, ajvErrors, ajvConfig : { useDefaults : true } , schemaName} ) ;
102+ } ) ( ) ;
103+ }
104+
105+ // When AJV is configured with useDefaults: true, it may add properties to the
106+ // provided configuration that were not initially present. This behavior can
107+ // lead to unexpected side effects and potential issues. To avoid these
108+ // problems, we create a copy of the configuration. If we need the altered
109+ // configuration later, we return this copied version.
110+ const optionsCopy = structuredClone ( options ) ;
111+ const schemaValidator = await defaultsValidator [ schemaName ] ;
112+ await schemaValidator . validate ( optionsCopy ) ;
113+
114+ return optionsCopy ;
115+ }
116+
94117/**
95118 * Validates the given ui5 configuration.
96119 *
@@ -114,6 +137,52 @@ export async function validate(options) {
114137 await _validate ( "ui5" , options ) ;
115138}
116139
140+ /**
141+ * Validates the given ui5 configuration and returns default values if none are provided.
142+ *
143+ * @public
144+ * @function
145+ * @static
146+ * @param {object } options
147+ * @param {object } options.config The UI5 Configuration to validate
148+ * @param {object } options.project Project information
149+ * @param {string } options.project.id ID of the project
150+ * @param {object } [options.yaml] YAML information
151+ * @param {string } options.yaml.path Path of the YAML file
152+ * @param {string } options.yaml.source Content of the YAML file
153+ * @param {number } [options.yaml.documentIndex=0] Document index in case the YAML file contains multiple documents
154+ * @throws {module:@ui5/project/validation/ValidationError }
155+ * Rejects with a {@link @ui5/project/validation/ValidationError ValidationError }
156+ * when the validation fails.
157+ * @returns {Promise<options> } Returns a Promise that resolves when the validation succeeds
158+ */
159+ export async function getDefaults ( options ) {
160+ return await _validateAndSetDefaults ( "ui5" , options ) ;
161+ }
162+
163+ /**
164+ * Enhances bundleDefinition by adding missing properties with their respective default values.
165+ *
166+ * @param {object[] } bundles Bundles to be enhanced
167+ * @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleDefinition } bundles[].bundleDefinition
168+ * Module bundle definition
169+ * @param {module:@ui5/builder/processors/bundlers/moduleBundler~ModuleBundleOptions } [bundles[].bundleOptions]
170+ * Module bundle options
171+ * @param {module:@ui5/project/specifications/Project } project The project to get metadata from
172+ * @returns {Promise<object> } The enhanced BundleDefinition & BundleOptions
173+ */
174+ export async function enhanceBundlesWithDefaults ( bundles , project ) {
175+ const config = {
176+ specVersion : `${ project . getSpecVersion ( ) } ` ,
177+ type : `${ project . getType ( ) } ` ,
178+ metadata : { name : project . getName ( ) } ,
179+ builder : { bundles}
180+ } ;
181+ const result = await getDefaults ( { config, project : { id : project . getName ( ) } } ) ;
182+
183+ return result . config . builder . bundles ;
184+ }
185+
117186/**
118187 * Validates the given ui5-workspace configuration.
119188 *
0 commit comments