import { ParserOptions } from './options' ; const illegalCharacterRegex = /[.#@/"'*%]+/g ; const startsWithNumberRegex = /^\d+/g ; function makeSchemaNameValid ( name ) { while ( name . match ( illegalCharacterRegex ) ) { name = name . replace ( illegalCharacterRegex , '' ) ; } if ( name . match ( startsWithNumberRegex ) ) { name = `schema${ name }` ; } return name ; } param names param options returns internal export function ensureValidSchemaNames ( names , options ) { if ( options . strictNaming ) { validateSchemaNames ( names ) ; } return names . map ( name => makeSchemaNameValid ( name ) ) ; } function validateSchemaNames ( names ) { const namesStartingWithNumber = names . filter ( name => name . match ( startsWithNumberRegex ) ) ; const namesContainingIllegalChars = names . filter ( name => name . match ( illegalCharacterRegex ) ) ; const hasInvalidNames = namesStartingWithNumber . length || namesContainingIllegalChars . length ; if ( hasInvalidNames ) { throw new Error ( getErrorMessage ( namesStartingWithNumber , namesContainingIllegalChars ) ) ; } } function getErrorMessage ( namesStartingWithNumber , namesContainingIllegalChars ) { let message = 'The service specification contains invalid schema names. Adjust the definition file or enable automatic name adjustment with `skipValidation`.' ; if ( namesStartingWithNumber . length ) { message += `\n\tInvalid names starting with a number: ${ namesStartingWithNumber . map ( name => `'${ name }'` ) }` ; } if ( namesContainingIllegalChars . length ) { message += `\n\tInvalid names containing illegal characters: ${ namesContainingIllegalChars . map ( name => `'${ name }'` ) }` ; } return message ; }