Skip to content

Commit

Permalink
update sanitize function so that it works when schema is an unindente…
Browse files Browse the repository at this point in the history
…d json string
  • Loading branch information
crazyo committed Nov 17, 2022
1 parent 26e9083 commit 93ba6f1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 12 deletions.
10 changes: 8 additions & 2 deletions sanitize.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cfschema

import (
"bufio"
"bytes"
"encoding/json"
"regexp"
"strings"
Expand All @@ -14,9 +15,14 @@ var (

// Sanitize returns a sanitized copy of the specified JSON Schema document.
// The sanitized copy works around any problems with JSON Schema regex validation by
// - Rewriting all patternProperty regexes to the empty string (the regex is never used anyway)
// - Rewriting all unsupported (valid for ECMA-262 but not for Go) pattern regexes to the empty string
// - Rewriting all patternProperty regexes to the empty string (the regex is never used anyway)
// - Rewriting all unsupported (valid for ECMA-262 but not for Go) pattern regexes to the empty string
func Sanitize(document string) (string, error) {
var formattedJSON bytes.Buffer
if err := json.Indent(&formattedJSON, []byte(document), "", " "); err != nil {
return "", err
}
document = string(formattedJSON.Bytes())
document = sanitizePatternProperties.ReplaceAllString(document, `$1""`)

var sb strings.Builder
Expand Down
66 changes: 56 additions & 10 deletions sanitize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func TestSanitize(t *testing.T) {
"type": "string",
"minLength": 1,
"maxLength": 512
},
}
}
`,
SanitizedDocument: `
Expand All @@ -32,7 +32,7 @@ func TestSanitize(t *testing.T) {
"type": "string",
"minLength": 1,
"maxLength": 512
},
}
}
`,
},
Expand Down Expand Up @@ -92,15 +92,15 @@ func TestSanitize(t *testing.T) {
"KmsKeyId": {
"description": "The Amazon Resource Name (ARN) of the CMK to use when encrypting log data.",
"type": "string",
"pattern":"",
"pattern": "",
"maxLength": 256
},
"Key" : {
"type" : "string",
"pattern" : "",
"description" : "The key name of the tag. You can specify a value that is 1 to 128 Unicode characters in length and cannot be prefixed with aws:. You can use any of the following characters: the set of Unicode letters, digits, whitespace, _, ., /, =, +, and -.",
"minLength" : 1,
"maxLength" : 128
"Key": {
"type": "string",
"pattern": "",
"description": "The key name of the tag. You can specify a value that is 1 to 128 Unicode characters in length and cannot be prefixed with aws:. You can use any of the following characters: the set of Unicode letters, digits, whitespace, _, ., /, =, +, and -.",
"minLength": 1,
"maxLength": 128
},
"VirtualMfaDeviceName": {
"minLength": 1,
Expand Down Expand Up @@ -164,9 +164,55 @@ func TestSanitize(t *testing.T) {
"": {
"type": "string"
},
"additionalProperties": false
"additionalProperties": false
}
}
}
`,
},
{
TestDescription: "Sanitize unindented JSON",
InputDocument: `{"LogGroupName":{"description":"The name of the log group. If you don't specify a name, AWS CloudFormation generates a unique ID for the log group.","type":"string","minLength":1,"maxLength":512,"pattern":"^[.\\-_/#A-Za-z0-9]{1,512}\\Z"},"KmsKeyId":{"description":"The Amazon Resource Name (ARN) of the CMK to use when encrypting log data.","type":"string","pattern":"^arn:[a-z0-9-]+:kms:[a-z0-9-]+:\\d{12}:(key|alias)/.+\\Z","maxLength":256},"Key":{"type":"string","pattern":"^(?!aws:)[a-zA-Z+-=._:/]+$","description":"The key name of the tag. You can specify a value that is 1 to 128 Unicode characters in length and cannot be prefixed with aws:. You can use any of the following characters: the set of Unicode letters, digits, whitespace, _, ., /, =, +, and -.","minLength":1,"maxLength":128},"VirtualMfaDeviceName":{"minLength":1,"maxLength":226,"pattern":"[\\w+=,.@-]+","type":"string"},"Path":{"minLength":1,"maxLength":512,"pattern":"(\\u002F)|(\\u002F[\\u0021-\\u007F]+\\u002F)","type":"string"},"SerialNumber":{"minLength":9,"maxLength":256,"pattern":"[\\w+=/:,.@-]+","type":"string"}}`,
SanitizedDocument: `
{
"LogGroupName": {
"description": "The name of the log group. If you don't specify a name, AWS CloudFormation generates a unique ID for the log group.",
"type": "string",
"minLength": 1,
"maxLength": 512,
"pattern": ""
},
"KmsKeyId": {
"description": "The Amazon Resource Name (ARN) of the CMK to use when encrypting log data.",
"type": "string",
"pattern": "",
"maxLength": 256
},
"Key": {
"type": "string",
"pattern": "",
"description": "The key name of the tag. You can specify a value that is 1 to 128 Unicode characters in length and cannot be prefixed with aws:. You can use any of the following characters: the set of Unicode letters, digits, whitespace, _, ., /, =, +, and -.",
"minLength": 1,
"maxLength": 128
},
"VirtualMfaDeviceName": {
"minLength": 1,
"maxLength": 226,
"pattern": "[\\w+=,.@-]+",
"type": "string"
},
"Path": {
"minLength": 1,
"maxLength": 512,
"pattern": "",
"type": "string"
},
"SerialNumber": {
"minLength": 9,
"maxLength": 256,
"pattern": "[\\w+=/:,.@-]+",
"type": "string"
}
}
`,
},
Expand Down

0 comments on commit 93ba6f1

Please sign in to comment.