-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds sanitizeJsonAsString (#410)
* feat: Adds sanitizeJsonAsString * fix: Removes unused import
- Loading branch information
Showing
6 changed files
with
145 additions
and
7 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,48 @@ | ||
# sanitizeJsonAsString | ||
|
||
[Back to root readme.md](../../../readme.md) | ||
|
||
This function uses `rulr.isJsonAsString` and can be used when you want to sanitize an input to be a string containing JSON as shown in the example below. This function is a higher order rule as it uses a sub-rule to validate input that is JSON. This function should only throw `rulr.InvalidJsonAsStringError`, `SyntaxError`, and errors from the sub-rule. | ||
|
||
```ts | ||
import * as rulr from 'rulr' | ||
|
||
const constrainToExample = rulr.object({ | ||
required: { | ||
example: rulr.sanitizeJsonAsString( | ||
rulr.object({ | ||
required: { | ||
someProp: rulr.positiveInteger, | ||
}, | ||
}) | ||
), | ||
}, | ||
}) | ||
|
||
type Example = rulr.Static<typeof constrainToExample> | ||
// { | ||
// example: { | ||
// someProp: rulr.PositiveInteger | ||
// } | ||
// } | ||
|
||
// Valid | ||
const example1: Example = constrainToExample({ | ||
example: '{ "someProp": 1 }', | ||
}) | ||
|
||
// Invalid: Not a positive integer - InvalidPositiveIntegerError | ||
const example2: Example = constrainToExample({ | ||
example: '{ "someProp": -1 }', | ||
}) | ||
|
||
// Invalid: Not JSON - SyntaxError | ||
const example3: Example = constrainToExample({ | ||
example: 'abc', | ||
}) | ||
|
||
// Invalid: Not a string - InvalidJsonAsStringError | ||
const example4: Example = constrainToExample({ | ||
example: 1, | ||
}) | ||
``` |
48 changes: 48 additions & 0 deletions
48
src/sanitizationRules/sanitizeJsonAsString/sanitizeJsonAsString.test.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,48 @@ | ||
import * as assert from 'assert' | ||
import * as rulr from '../../rulr' | ||
|
||
test('sanitizeJsonAsString should allow JSON as a string', () => { | ||
interface Output { | ||
someProp: number | ||
} | ||
const input = '{ "someProp": 1 }' | ||
const rule = rulr.sanitizeJsonAsString( | ||
rulr.object({ | ||
required: { | ||
someProp: rulr.number, | ||
}, | ||
}) | ||
) | ||
const output: Output = rule(input) | ||
assert.deepStrictEqual(output, { someProp: 1 }) | ||
}) | ||
|
||
test('sanitizeJsonAsString should now allow value that does not match the sub rule', () => { | ||
const input = 'true' | ||
const rule = rulr.sanitizeJsonAsString(rulr.number) | ||
assert.throws(() => rule(input), rulr.InvalidNumberError) | ||
}) | ||
|
||
test('sanitizeJsonAsString should now allow value that is not JSON as a string', () => { | ||
const input = 'abc' | ||
const rule = rulr.sanitizeJsonAsString(rulr.number) | ||
assert.throws(() => rule(input), SyntaxError) | ||
}) | ||
|
||
test('sanitizeJsonAsString should now allow value that is not a string', () => { | ||
const input = 1 | ||
const rule = rulr.sanitizeJsonAsString(rulr.number) | ||
assert.throws(() => rule(input), rulr.InvalidJsonAsStringError) | ||
}) | ||
|
||
test('isJsonAsString should return true for valid JSON', () => { | ||
assert.strictEqual(rulr.isJsonAsString('{ "someProp": 1 }'), true) | ||
}) | ||
|
||
test('isJsonAsString should return false when the value is not a string', () => { | ||
assert.strictEqual(rulr.isJsonAsString(1), false) | ||
}) | ||
|
||
test('isJsonAsString should return false when the value is not JSON', () => { | ||
assert.strictEqual(rulr.isJsonAsString('abc'), false) | ||
}) |
35 changes: 35 additions & 0 deletions
35
src/sanitizationRules/sanitizeJsonAsString/sanitizeJsonAsString.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,35 @@ | ||
import { BaseError } from 'make-error' | ||
import { isString } from '../../valueRules/string/string' | ||
import { Constrained, Rule } from '../../core' | ||
|
||
export class InvalidJsonAsStringError extends BaseError { | ||
constructor() { | ||
super('expected JSON as string') | ||
} | ||
} | ||
|
||
export const jsonAsStringSymbol = Symbol() | ||
|
||
export type JsonAsString = Constrained<typeof jsonAsStringSymbol, string> | ||
|
||
function isJson(input: string) { | ||
try { | ||
JSON.parse(input) | ||
return true | ||
} catch { | ||
return false | ||
} | ||
} | ||
|
||
export function isJsonAsString(input: unknown): input is JsonAsString { | ||
return isString(input) && isJson(input) | ||
} | ||
|
||
export function sanitizeJsonAsString<T>(jsonRule: Rule<T>) { | ||
return (input: unknown) => { | ||
if (isString(input)) { | ||
return jsonRule(JSON.parse(input)) | ||
} | ||
throw new InvalidJsonAsStringError() | ||
} | ||
} |
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