@@ -40,7 +40,8 @@ const parser = new Parser({
4040 resolver : {
4141 cache : false ,
4242 }
43- }
43+ } ,
44+
4445} ) ;
4546
4647parser . registerSchemaParser ( AvroSchemaParser ( ) ) ;
@@ -86,10 +87,73 @@ export interface ValidateOptions {
8687 'diagnostics-format' ?: `${OutputFormat } `;
8788 'fail-severity' ?: SeverityKind ;
8889 'output' ?: string ;
90+ suppressWarnings ?: string [ ] ;
91+ suppressAllWarnings ?: boolean ;
8992}
9093
91- export async function validate ( command : Command , specFile : Specification , options : ValidateOptions = { } ) {
92- const diagnostics = await parser . validate ( specFile . text ( ) , { source : specFile . getSource ( ) } ) ;
94+ export async function validate (
95+ command : Command ,
96+ specFile : Specification ,
97+ options : ValidateOptions = { }
98+ ) {
99+ const suppressAllWarnings = options . suppressAllWarnings ?? false ;
100+ const suppressedWarnings = options . suppressWarnings ?? [ ] ;
101+ let activeParser : Parser ;
102+
103+ // Helper to build a parser with given rules turned off
104+ const buildCustomParser = ( rulesToSuppress : string [ ] ) =>
105+ new Parser ( {
106+ ruleset : {
107+ extends : [ ] ,
108+ rules : Object . fromEntries ( rulesToSuppress . map ( rule => [ rule , 'off' ] ) ) ,
109+ } ,
110+ __unstable : {
111+ resolver : {
112+ cache : false ,
113+ } ,
114+ } ,
115+ } ) ;
116+
117+ if ( suppressAllWarnings ) {
118+ // Run the default parser to discover all rule codes
119+ const diagnostics = await parser . validate ( specFile . text ( ) , {
120+ source : specFile . getSource ( ) ,
121+ } ) ;
122+ const allRuleNames = Array . from (
123+ new Set ( diagnostics . map ( d => d . code ) . filter ( ( c ) : c is string => typeof c === 'string' ) )
124+ ) ;
125+ activeParser = buildCustomParser ( allRuleNames ) ;
126+ } else if ( suppressedWarnings . length === 0 ) {
127+ activeParser = parser ;
128+ } else {
129+ try {
130+ activeParser = buildCustomParser ( suppressedWarnings ) ;
131+ } catch ( e : any ) {
132+ const msg = e . message || '' ;
133+ const matches = [ ...msg . matchAll ( / C a n n o t e x t e n d n o n - e x i s t i n g r u l e : " ( [ ^ " ] + ) " / g) ] ;
134+ const invalidRules = matches . map ( m => m [ 1 ] ) ;
135+ if ( invalidRules . length > 0 ) {
136+ for ( const rule of invalidRules ) {
137+ command . log ( `Warning: '${ rule } ' is not a known rule and will be ignored.` ) ;
138+ }
139+ const validRules = suppressedWarnings . filter ( rule => ! invalidRules . includes ( rule ) ) ;
140+ activeParser = buildCustomParser ( validRules ) ;
141+ } else {
142+ throw e ;
143+ }
144+ }
145+ }
146+
147+ // Register schema parsers
148+ activeParser . registerSchemaParser ( AvroSchemaParser ( ) ) ;
149+ activeParser . registerSchemaParser ( OpenAPISchemaParser ( ) ) ;
150+ activeParser . registerSchemaParser ( RamlDTSchemaParser ( ) ) ;
151+ activeParser . registerSchemaParser ( ProtoBuffSchemaParser ( ) ) ;
152+
153+ const diagnostics = await activeParser . validate ( specFile . text ( ) , {
154+ source : specFile . getSource ( ) ,
155+ } ) ;
156+
93157 return logDiagnostics ( diagnostics , command , specFile , options ) ;
94158}
95159
0 commit comments