Skip to content

Commit

Permalink
fix(validate): rename validations -> validate
Browse files Browse the repository at this point in the history
  • Loading branch information
gamemaker1 committed Jul 16, 2023
1 parent cab0b3b commit ee5c18a
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ const limiter = rateLimit({
})
```

### `validation`
### `validate`

> `boolean`
Expand Down
22 changes: 11 additions & 11 deletions source/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ type Configuration = {
skip: ValueDeterminingMiddleware<boolean>
requestWasSuccessful: ValueDeterminingMiddleware<boolean>
store: Store
validation: boolean
validate: boolean
}

/**
Expand Down Expand Up @@ -147,7 +147,7 @@ const omitUndefinedOptions = (
*/
const parseOptions = (
passedOptions: Partial<Options>,
validate: Validations,
validations: Validations,
): Configuration => {
// Passing undefined should be equivalent to not passing an option at all, so we'll
// omit all fields where their value is undefined.
Expand All @@ -172,9 +172,9 @@ const parseOptions = (
keyGenerator(request: Request, _response: Response): string {
// Run the validation checks on the IP and headers to make sure everything
// is working as intended.
validate.ip(request.ip)
validate.trustProxy(request)
validate.xForwardedForHeader(request)
validations.ip(request.ip)
validations.trustProxy(request)
validations.xForwardedForHeader(request)

// By default, use the IP address to rate limit users.
return request.ip
Expand Down Expand Up @@ -206,8 +206,8 @@ const parseOptions = (
_response: Response,
_optionsUsed: Options,
): void {},
// Will print an error to the console if a few known misconfigurations are detected.
validation: true,
// Print an error to the console if a few known misconfigurations are detected.
validate: true,
// Allow the options object to be overriden by the options passed to the middleware.
...notUndefinedOptions,
// Note that this field is declared after the user's options are spread in,
Expand Down Expand Up @@ -266,9 +266,9 @@ const rateLimit = (
passedOptions?: Partial<Options>,
): RateLimitRequestHandler => {
// Create the validator before even parsing the rest of the options
const validate = new Validations(passedOptions?.validation ?? true)
const validations = new Validations(passedOptions?.validate ?? true)
// Parse the options and add the default values for unspecified options
const options = parseOptions(passedOptions ?? {}, validate)
const options = parseOptions(passedOptions ?? {}, validations)

// Call the `init` method on the store, if it exists
if (typeof options.store.init === 'function') options.store.init(options)
Expand Down Expand Up @@ -381,8 +381,8 @@ const rateLimit = (
options.onLimitReached(request, response, options)
}

// All validations should have run at least once by now
validate.disable()
// Disable the validations, since they should have run at least once by now.
validations.disable()

// If the client has exceeded their rate limit, set the Retry-After header
// and call the `handler` function
Expand Down
2 changes: 1 addition & 1 deletion source/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ export type Options = {
/**
* Whether or not the validation checks should run.
*/
readonly validation: boolean
readonly validate: boolean

/**
* Whether to send `X-RateLimit-*` headers with the rate limit and the number
Expand Down
40 changes: 20 additions & 20 deletions test/library/validation-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,78 @@ import {
import { Validations } from '../../source/validations.js'

describe('validations tests', () => {
let validate = new Validations(true)
let validations = new Validations(true)

beforeEach(() => {
jest.spyOn(console, 'error').mockImplementation(() => {})
})
afterEach(() => {
jest.restoreAllMocks()
validate = new Validations(true)
validations = new Validations(true)
})

describe('validate.ip', () => {
describe('ip', () => {
it('should allow a valid IPv4', () => {
validate.ip('1.2.3.4')
validations.ip('1.2.3.4')
expect(console.error).not.toBeCalled()
})

it('should allow a valid IPv6', () => {
validate.ip('1200:0000:AB00:1234:0000:2552:7777:1313')
validations.ip('1200:0000:AB00:1234:0000:2552:7777:1313')
expect(console.error).not.toBeCalled()
})

it('should log an error for an invalid IP', () => {
validate.ip('badip')
validations.ip('badip')
expect(console.error).toBeCalled()
})

it('shoud log an error for an undefined IP', () => {
validate.ip(undefined)
validations.ip(undefined)
expect(console.error).toBeCalled()
})

it('should log an error for an IPv4 with a port', () => {
validate.ip('1.2.3.4:1234')
validations.ip('1.2.3.4:1234')
expect(console.error).toBeCalled()
})

it('should log an error for an IPv6 with a port', () => {
validate.ip('[1200:0000:AB00:1234:0000:2552:7777:1313]:1234')
validations.ip('[1200:0000:AB00:1234:0000:2552:7777:1313]:1234')
expect(console.error).toBeCalled()
})
})

describe('trustProxy', () => {
it('should log an error on "trust proxy" = true', () => {
validate.trustProxy({ app: { get: () => true } } as any)
validations.trustProxy({ app: { get: () => true } } as any)
expect(console.error).toBeCalled()
})
it('should not log an error on "trust proxy" != true', () => {
validate.trustProxy({ app: { get: () => false } } as any)
validate.trustProxy({ app: { get: () => '1.2.3.4' } } as any)
validate.trustProxy({ app: { get: () => /1.2.3.4/ } } as any)
validate.trustProxy({ app: { get: () => ['1.2.3.4'] } } as any)
validations.trustProxy({ app: { get: () => false } } as any)
validations.trustProxy({ app: { get: () => '1.2.3.4' } } as any)
validations.trustProxy({ app: { get: () => /1.2.3.4/ } } as any)
validations.trustProxy({ app: { get: () => ['1.2.3.4'] } } as any)
expect(console.error).not.toBeCalled()
})
})

describe('xForwardedFor', () => {
it('should log an error only with X-Forwarded-For header and "trust proxy" = false', () => {
validate.xForwardedForHeader({
validations.xForwardedForHeader({
app: { get: () => true },
headers: { 'x-forwarded-for': '1.2.3.4' },
} as any)
validate.xForwardedForHeader({
validations.xForwardedForHeader({
app: { get: () => true },
headers: {},
} as any)
validate.xForwardedForHeader({
validations.xForwardedForHeader({
app: { get: () => false },
headers: {},
} as any)
expect(console.error).not.toBeCalled()
validate.xForwardedForHeader({
validations.xForwardedForHeader({
app: { get: () => false },
headers: { 'x-forwarded-for': '1.2.3.4' },
} as any)
Expand All @@ -99,8 +99,8 @@ describe('validations tests', () => {
})

it('should do nothing after disable() is called', () => {
validate.disable()
validate.ip('badip')
validations.disable()
validations.ip('badip')
expect(console.error).not.toBeCalled()
})
})
Expand Down

0 comments on commit ee5c18a

Please sign in to comment.