@@ -11,6 +11,14 @@ import langtags from "./imports/langtags.js";
1111import { validateMITLicense } from "@keymanapp/developer-utils" ;
1212import { KmpCompiler } from "@keymanapp/kmc-package" ;
1313
14+ import AjvModule from 'ajv' ;
15+ import AjvFormatsModule from 'ajv-formats' ;
16+ const Ajv = AjvModule . default ; // The actual expected Ajv type.
17+ const ajvFormats = AjvFormatsModule . default ;
18+
19+ import { Schemas } from "@keymanapp/common-types" ;
20+ import { packageKeysExamplesToKeyboardInfo } from "./example-keys.js" ;
21+
1422const regionNames = new Intl . DisplayNames ( [ 'en' ] , { type : "region" } ) ;
1523const scriptNames = new Intl . DisplayNames ( [ 'en' ] , { type : "script" } ) ;
1624const langtagsByTag = { } ;
@@ -281,6 +289,20 @@ export class KeyboardInfoCompiler {
281289 }
282290
283291 const jsonOutput = JSON . stringify ( keyboard_info , null , 2 ) ;
292+
293+ // TODO: look at performance improvements by precompiling Ajv schemas on first use
294+ const ajv = new Ajv ( { logger : {
295+ log : ( message ) => this . callbacks . reportMessage ( KeyboardInfoCompilerMessages . Hint_OutputValidation ( { message} ) ) ,
296+ warn : ( message ) => this . callbacks . reportMessage ( KeyboardInfoCompilerMessages . Warn_OutputValidation ( { message} ) ) ,
297+ error : ( message ) => this . callbacks . reportMessage ( KeyboardInfoCompilerMessages . Error_OutputValidation ( { message} ) ) ,
298+ } } ) ;
299+ ajvFormats . default ( ajv ) ;
300+ if ( ! ajv . validate ( Schemas . default . keyboard_info , keyboard_info ) ) {
301+ // This is an internal fatal error; we should not be capable of producing
302+ // invalid output, so it is best to throw and die
303+ throw new Error ( ajv . errorsText ( ) ) ;
304+ }
305+
284306 return new TextEncoder ( ) . encode ( jsonOutput ) ;
285307 }
286308
@@ -380,9 +402,9 @@ export class KeyboardInfoCompiler {
380402 if ( example . id == bcp47 ) {
381403 language . examples . push ( {
382404 // we don't copy over example.id
383- keys :example . keys ,
384- note :example . note ,
385- text :example . text
405+ keys : packageKeysExamplesToKeyboardInfo ( example . keys ) ,
406+ note : example . note ,
407+ text : example . text
386408 } ) ;
387409 }
388410 }
0 commit comments