-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: add ValidationException #694
Open
marcogrcr
wants to merge
2
commits into
contentful:main
Choose a base branch
from
marcogrcr:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+236
−72
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 was deleted.
Oops, something went wrong.
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,78 @@ | ||
import * as runtypes from 'runtypes' | ||
import { proxyValidationError } from './proxy-validation-error' | ||
|
||
const MethodValidator = proxyValidationError( | ||
runtypes.Union( | ||
runtypes.Literal('GET'), | ||
runtypes.Literal('PATCH'), | ||
runtypes.Literal('HEAD'), | ||
runtypes.Literal('POST'), | ||
runtypes.Literal('DELETE'), | ||
runtypes.Literal('OPTIONS'), | ||
runtypes.Literal('PUT'), | ||
), | ||
) | ||
|
||
const PathValidator = proxyValidationError( | ||
runtypes.String.withConstraint((s) => s.startsWith('/'), { | ||
name: 'CanonicalURI', | ||
}), | ||
) | ||
|
||
const SignatureValidator = proxyValidationError( | ||
runtypes.String.withConstraint((s) => s.length === 64, { | ||
name: 'SignatureLength', | ||
}), | ||
) | ||
|
||
export const CanonicalRequestValidator = proxyValidationError( | ||
runtypes | ||
.Record({ | ||
method: MethodValidator, | ||
path: PathValidator, | ||
}) | ||
.And( | ||
runtypes.Partial({ | ||
headers: runtypes.Dictionary(runtypes.String, 'string'), | ||
body: runtypes.String, | ||
}), | ||
), | ||
) | ||
export type CanonicalRequest = runtypes.Static<typeof CanonicalRequestValidator> | ||
|
||
export const SecretValidator = proxyValidationError( | ||
runtypes.String.withConstraint((s) => s.length === 64, { | ||
name: 'SecretLength', | ||
}), | ||
) | ||
export type Secret = runtypes.Static<typeof SecretValidator> | ||
|
||
// Only dates after 01-01-2020 | ||
export const TimestampValidator = proxyValidationError( | ||
runtypes.Number.withConstraint((n) => n > 1577836800000, { | ||
name: 'TimestampAge', | ||
}), | ||
) | ||
export type Timestamp = runtypes.Static<typeof TimestampValidator> | ||
|
||
const SignedHeadersValidator = proxyValidationError( | ||
runtypes | ||
.Array(runtypes.String) | ||
.withConstraint((l) => l.length >= 2, { name: 'MissingTimestampOrSignedHeaders' }), | ||
) | ||
|
||
export const RequestMetadataValidator = proxyValidationError( | ||
runtypes.Record({ | ||
signature: SignatureValidator, | ||
timestamp: TimestampValidator, | ||
signedHeaders: SignedHeadersValidator, | ||
}), | ||
) | ||
export type RequestMetadata = runtypes.Static<typeof RequestMetadataValidator> | ||
|
||
export const TimeToLiveValidator = proxyValidationError( | ||
runtypes.Number.withConstraint((n) => n >= 0, { | ||
name: 'PositiveNumber', | ||
}), | ||
) | ||
export type TimeToLive = runtypes.Static<typeof TimeToLiveValidator> |
88 changes: 88 additions & 0 deletions
88
src/requests/typings/validators/proxy-validation-error.spec.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,88 @@ | ||
import * as assert from 'assert' | ||
import * as runtypes from 'runtypes' | ||
import { ValidationException } from '../../exceptions' | ||
import { proxyValidationError } from './proxy-validation-error' | ||
|
||
describe('proxyValidationError', () => { | ||
describe('scalar without named constraint', () => { | ||
it('converts to ValidationException', () => { | ||
assert.throws(() => { | ||
try { | ||
proxyValidationError( | ||
runtypes.Union(runtypes.Literal('val1'), runtypes.Literal('val2')), | ||
).check('invalid') | ||
} catch (error) { | ||
if (error instanceof ValidationException) { | ||
assert.strictEqual(error.constraintName, undefined) | ||
assert.strictEqual(error.key, undefined) | ||
} | ||
|
||
throw error | ||
} | ||
}, ValidationException) | ||
}) | ||
}) | ||
|
||
describe('scalar with named constraint', () => { | ||
it('converts to ValidationException without constraint name', () => { | ||
assert.throws(() => { | ||
try { | ||
proxyValidationError( | ||
runtypes.String.withConstraint((s) => s === 'value', { name: 'constraint-name' }), | ||
).check('invalid') | ||
} catch (error) { | ||
if (error instanceof ValidationException) { | ||
assert.strictEqual(error.constraintName, 'constraint-name') | ||
assert.strictEqual(error.key, undefined) | ||
} | ||
|
||
throw error | ||
} | ||
}, ValidationException) | ||
}) | ||
}) | ||
|
||
describe('object without named constraint', () => { | ||
it('converts to ValidationException with key', () => { | ||
assert.throws(() => { | ||
try { | ||
proxyValidationError( | ||
runtypes.Record({ | ||
field: runtypes.Union(runtypes.Literal('val1'), runtypes.Literal('val2')), | ||
}), | ||
).check({ field: 'invalid' }) | ||
} catch (error) { | ||
if (error instanceof ValidationException) { | ||
assert.strictEqual(error.constraintName, undefined) | ||
assert.strictEqual(error.key, 'field') | ||
} | ||
|
||
throw error | ||
} | ||
}, ValidationException) | ||
}) | ||
}) | ||
|
||
describe('object with named constraint', () => { | ||
it('converts to ValidationException with key and constraint name', () => { | ||
assert.throws(() => { | ||
try { | ||
proxyValidationError( | ||
runtypes.Record({ | ||
field: runtypes.String.withConstraint((s) => s === 'value', { | ||
name: 'constraint-name', | ||
}), | ||
}), | ||
).check({ field: 'invalid' }) | ||
} catch (error) { | ||
if (error instanceof ValidationException) { | ||
assert.strictEqual(error.constraintName, 'constraint-name') | ||
assert.strictEqual(error.key, 'field') | ||
} | ||
|
||
throw error | ||
} | ||
}, ValidationException) | ||
}) | ||
}) | ||
}) |
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,37 @@ | ||
import * as runtypes from 'runtypes' | ||
import { ValidationException } from '../../exceptions' | ||
|
||
const NAMED_CONSTRAINT_FAILURE_MSG = /^Failed (.+?) check/ | ||
|
||
// eslint-disable-next-line no-unused-vars | ||
export function proxyValidationError<T extends object>(constraint: T): T { | ||
// eslint-disable-next-line no-undef | ||
return new Proxy(constraint, { | ||
get(target, property) { | ||
const value = target[property as keyof T] | ||
if (typeof value !== 'function') { | ||
return value | ||
} | ||
|
||
return (...args: unknown[]) => { | ||
try { | ||
return value(...args) | ||
} catch (error) { | ||
if (error instanceof runtypes.ValidationError) { | ||
let constraintName = undefined | ||
if ('name' in constraint && typeof constraint.name === 'string') { | ||
constraintName = constraint.name | ||
} else { | ||
const result = NAMED_CONSTRAINT_FAILURE_MSG.exec(error.message) | ||
constraintName = result ? result[1] : undefined | ||
} | ||
|
||
throw new ValidationException(error.message, constraintName, error.key) | ||
} | ||
|
||
throw error | ||
} | ||
} | ||
}, | ||
}) | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: This is the same as
src/typings/validators.ts
. The only difference is that all objects are wrapped inproxyValidationError()
.