-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add checks for max length of fields in CIP-0119
- Loading branch information
Showing
5 changed files
with
59 additions
and
4 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
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
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,22 @@ | ||
module Cardano.Api.GeneralParsers (textWithMaxLength) where | ||
|
||
import Data.Aeson.Types (Parser, Value, parseJSON) | ||
import Data.Text (Text) | ||
import qualified Data.Text as T | ||
|
||
-- | Parser for 'Text' that validates that the number of characters is | ||
-- under a given maximum. The 'String' parameter is meant to be the name | ||
-- of the field in order to be able to give context in case of error. | ||
textWithMaxLength :: String -> Int -> Value -> Parser Text | ||
textWithMaxLength fieldName maxLen value = do | ||
txt <- parseJSON value | ||
if T.length txt <= maxLen | ||
then pure txt | ||
else | ||
fail $ | ||
"key \"" | ||
++ fieldName | ||
++ "\" exceeds maximum length of " | ||
++ show maxLen | ||
++ " characters. Got length: " | ||
++ show (T.length txt) |
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
17 changes: 17 additions & 0 deletions
17
...est/cardano-api-test/files/input/gov-anchor-data/too-long-given-name-drep-metadata.jsonld
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,17 @@ | ||
{ | ||
"@context": { | ||
"CIP100": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0100/README.md#", | ||
"CIP119": "https://github.com/cardano-foundation/CIPs/blob/master/CIP-0119/README.md#", | ||
"hashAlgorithm": "CIP100:hashAlgorithm", | ||
"body": { | ||
"@id": "CIP119:body", | ||
"@context": { | ||
"givenName": "CIP119:givenName" | ||
} | ||
} | ||
}, | ||
"hashAlgorithm": "blake2b-256", | ||
"body": { | ||
"givenName": "This name is way too long for it to fit in this field, so it should be rejected by the API" | ||
} | ||
} |