-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dphuang2
committed
Aug 25, 2024
1 parent
3e9fa56
commit 6d72770
Showing
2 changed files
with
46 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
generator/konfig-dash/packages/konfig-cli/src/util/spec-uses-pattern-string.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { OpenAPIV3_XDocument, recurseObject } from 'konfig-lib' | ||
import * as yaml from 'js-yaml' | ||
|
||
export function specUsesPatternString(specString: string): boolean { | ||
let spec: OpenAPIV3_XDocument | ||
|
||
try { | ||
// Try parsing as JSON first | ||
spec = JSON.parse(specString) | ||
} catch { | ||
// If JSON parsing fails, try parsing as YAML | ||
try { | ||
spec = yaml.load(specString) as OpenAPIV3_XDocument | ||
} catch { | ||
throw new Error('Invalid spec format. Must be valid JSON or YAML.') | ||
} | ||
} | ||
|
||
let hasPatternString = false | ||
|
||
recurseObject(spec, ({ value }) => { | ||
if ( | ||
typeof value === 'object' && | ||
value !== null && | ||
'type' in value && | ||
value.type === 'string' && | ||
'pattern' in value && | ||
typeof value.pattern === 'string' && | ||
value.pattern.length > 0 | ||
) { | ||
hasPatternString = true | ||
} | ||
}) | ||
|
||
return hasPatternString | ||
} |