11import { fileURLToPath } from "node:url" ;
22import { readFile } from "node:fs/promises" ;
33
4- async function loadSchema ( schemaPath ) {
5- const filePath = schemaPath . replace ( "http://ui5.sap/schema/" , "" ) ;
6- const schemaFile = await readFile (
7- fileURLToPath ( new URL ( `./schema/${ filePath } ` , import . meta. url ) ) , { encoding : "utf8" }
8- ) ;
9- return JSON . parse ( schemaFile ) ;
10- }
4+ /**
5+ * @module @ui 5/project/validation/validator
6+ * @description A collection of validation related APIs
7+ * @public
8+ */
9+
10+ /**
11+ * @enum {string}
12+ * @private
13+ * @readonly
14+ */
15+ export const SCHEMA_VARIANTS = {
16+ "ui5" : "ui5.json" ,
17+ "ui5-workspace" : "ui5-workspace.json"
18+ } ;
1119
1220class Validator {
13- constructor ( { Ajv, ajvErrors} ) {
21+ constructor ( { Ajv, ajvErrors, schemaName} ) {
22+ if ( ! schemaName && ! SCHEMA_VARIANTS [ schemaName ] ) {
23+ throw new Error (
24+ `"schemaName" is missing or incorrect. The available schemaName variants are ${ Object . keys (
25+ SCHEMA_VARIANTS
26+ ) . join ( ", " ) } `
27+ ) ;
28+ }
29+
30+ this . _schemaName = SCHEMA_VARIANTS [ schemaName ] ;
31+
1432 this . ajv = new Ajv ( {
1533 allErrors : true ,
1634 jsonPointers : true ,
17- loadSchema
35+ loadSchema : Validator . loadSchema
1836 } ) ;
1937 ajvErrors ( this . ajv ) ;
2038 }
2139
2240 _compileSchema ( ) {
41+ const schemaName = this . _schemaName ;
42+
2343 if ( ! this . _compiling ) {
2444 this . _compiling = Promise . resolve ( ) . then ( async ( ) => {
25- const schema = await loadSchema ( "ui5.json" ) ;
45+ const schema = await Validator . loadSchema ( schemaName ) ;
2646 const validate = await this . ajv . compileAsync ( schema ) ;
2747 return validate ;
2848 } ) ;
@@ -46,18 +66,33 @@ class Validator {
4666 } ) ;
4767 }
4868 }
69+
70+ static async loadSchema ( schemaPath ) {
71+ const filePath = schemaPath . replace ( "http://ui5.sap/schema/" , "" ) ;
72+ const schemaFile = await readFile (
73+ fileURLToPath ( new URL ( `./schema/${ filePath } ` , import . meta. url ) ) , { encoding : "utf8" }
74+ ) ;
75+ return JSON . parse ( schemaFile ) ;
76+ }
4977}
5078
51- let validator ;
79+ const validator = Object . create ( null ) ;
5280
53- /**
54- * @module @ui 5/project/validation/validator
55- * @description A collection of validation related APIs
56- * @public
57- */
81+ async function _validate ( schemaName , options ) {
82+ if ( ! validator [ schemaName ] ) {
83+ validator [ schemaName ] = ( async ( ) => {
84+ const { default : Ajv } = await import ( "ajv" ) ;
85+ const { default : ajvErrors } = await import ( "ajv-errors" ) ;
86+ return new Validator ( { Ajv, ajvErrors, schemaName} ) ;
87+ } ) ( ) ;
88+ }
89+
90+ const schemaValidator = await validator [ schemaName ] ;
91+ await schemaValidator . validate ( options ) ;
92+ }
5893
5994/**
60- * Validates the given configuration.
95+ * Validates the given ui5 configuration.
6196 *
6297 * @public
6398 * @function
@@ -76,12 +111,28 @@ let validator;
76111 * @returns {Promise<undefined> } Returns a Promise that resolves when the validation succeeds
77112 */
78113export async function validate ( options ) {
79- if ( ! validator ) {
80- const { default : Ajv } = await import ( "ajv" ) ;
81- const { default : ajvErrors } = await import ( "ajv-errors" ) ;
82- validator = new Validator ( { Ajv, ajvErrors} ) ;
83- }
84- await validator . validate ( options ) ;
114+ await _validate ( "ui5" , options ) ;
115+ }
116+
117+ /**
118+ * Validates the given ui5-workspace configuration.
119+ *
120+ * @public
121+ * @function
122+ * @static
123+ * @param {object } options
124+ * @param {object } options.config ui5-workspace Configuration to validate
125+ * @param {object } [options.yaml] YAML information
126+ * @param {string } options.yaml.path Path of the YAML file
127+ * @param {string } options.yaml.source Content of the YAML file
128+ * @param {number } [options.yaml.documentIndex=0] Document index in case the YAML file contains multiple documents
129+ * @throws {@ui5/project/validation/ValidationError }
130+ * Rejects with a {@link @ui5/project/validation/ValidationError ValidationError }
131+ * when the validation fails.
132+ * @returns {Promise<undefined> } Returns a Promise that resolves when the validation succeeds
133+ */
134+ export async function validateWorkspace ( options ) {
135+ await _validate ( "ui5-workspace" , options ) ;
85136}
86137
87138export {
0 commit comments