import { ISchemaInfo } from "./ISchemaInfo" ; import { DeploymentScopeKind } from "./scopes/DeploymentScopeKind" ; const schemaInfos = [ { normalizedSchema : '2014-04-01-preview/deploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . resourceGroup , isDeprecated : true } , { normalizedSchema : '2015-01-01/deploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . resourceGroup , isDeprecated : true } , { normalizedSchema : '2019-04-01/deploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . resourceGroup , isDeprecated : false } , { normalizedSchema : '2019-08-01/tenantDeploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . tenant , isDeprecated : false } , { normalizedSchema : '2018-05-01/subscriptionDeploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . subscription , isDeprecated : false } , { normalizedSchema : '2019-08-01/managementGroupDeploymentTemplate.json' , deploymentScopeKind : DeploymentScopeKind . managementGroup , isDeprecated : false } , ] ; let normalizedSchemaLCToSchemaInfoMap ; /**
 * Finds the ISchemaInfo that matches the given schema, if any
 */ export function findSchemaInfo ( schemaUri ) { if ( ! normalizedSchemaLCToSchemaInfoMap ) { normalizedSchemaLCToSchemaInfoMap = new Map < string , ISchemaInfo > ( ) ; for ( const si of schemaInfos ) { normalizedSchemaLCToSchemaInfoMap . set ( si . normalizedSchema . toLowerCase ( ) , si ) ; } } const extractedSchema = extractSchemaWithoutHttpOrHash ( schemaUri ) ; return extractedSchema ? normalizedSchemaLCToSchemaInfoMap . get ( extractedSchema . toLowerCase ( ) ) : undefined ; } /**
 * Given a schema value, removes the HTTP/S: prefix and the optional "#" affix
 *
 * E.g., given "https://2019-08-01/tenantDeploymentTemplate.json#" returns "2019-08-01/tenantDeploymentTemplate.json"
 */ function extractSchemaWithoutHttpOrHash ( schema ) { const matches = isArmSchemaRegex . exec ( schema ) ; const extractedSchema = matches ? matches [ 1 ] : undefined ; return extractedSchema ; } export type deprecatedSchemas = '2014-04-01-preview/deploymentTemplate.json' | '2015-01-01/deploymentTemplate.json' ; export type currentSchemas = '2019-04-01/deploymentTemplate.json' | '2019-08-01/tenantDeploymentTemplate.json' | '2018-05-01/subscriptionDeploymentTemplate.json' | '2019-08-01/managementGroupDeploymentTemplate.json' | '2019-08-01/tenantDeploymentTemplate.json' ; export type allSchemas = deprecatedSchemas | currentSchemas ; const containsArmSchemaRegexString = `https?:\/\/schema\.management\.azure\.com\/schemas\/([^"\/]+\/[a-zA-Z]*[dD]eploymentTemplate\.json)#?` ; const containsArmSchemaRegex = new RegExp ( containsArmSchemaRegexString , 'i' ) ; const isArmSchemaRegex = new RegExp ( `^${ containsArmSchemaRegexString }$` , 'i' ) ; const containsParametersSchemaRegexString = `https?:\/\/schema\.management\.azure\.com\/schemas\/[^"\/]+\/[a-zA-Z]*[dD]eploymentParameters\.json#?` ; const containsParametersSchemaRegex = new RegExp ( containsParametersSchemaRegexString , 'i' ) ; const isParametersSchemaRegex = new RegExp ( `^${ containsParametersSchemaRegexString }$` , 'i' ) ; export function containsArmSchema ( json ) { return ! ! json && containsArmSchemaRegex . test ( json ) ; } export function isArmSchema ( schema ) { return ! ! schema && isArmSchemaRegex . test ( schema ) ; } export function containsParametersSchema ( json ) { return ! ! json && containsParametersSchemaRegex . test ( json ) ; } export function isParametersSchema ( json ) { return ! ! json && isParametersSchemaRegex . test ( json ) ; } /**
 * Given a schema, returns the recommended one (most recent) for the same scope.
 * If the schema is not valid or there is no better schema, returns undefined
 */ export function getPreferredSchema ( schema ) { if ( schema . match ( /https?:\/\/schema\.management\.azure\.com\/schemas\/(2014-04-01-preview|2015-01-01)\/deploymentTemplate.json#?/i ) ) { return "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#" ; } return undefined ; }