Skip to content

Commit

Permalink
- reduced prettier width to 100
Browse files Browse the repository at this point in the history
- started work on issue #1
  • Loading branch information
robertbullen committed Dec 16, 2018
1 parent a79c2bc commit 5c8187a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"printWidth": 120,
"printWidth": 100,
"singleQuote": true,
"tabWidth": 4,
"trailingComma": "all"
Expand Down
63 changes: 60 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,79 @@ import * as yup from 'yup';

// Declare a `jwt()` method on the `StringSchema` interface via module augmentation.
declare module 'yup' {
export type JwtMode = 'regex' | 'decode' | 'verify';

export type JwtKey = string | Buffer;

export interface StringSchema {
jwt(message?: string): StringSchema;
jwt(mode: 'regex' | 'decode', message?: string): StringSchema;
jwt(mode: 'verify', key: JwtKey, message?: string): StringSchema;
}
}

// tslint:disable-next-line:no-invalid-template-strings
export const defaultMessage: string = '${path} must be a parsable JSON web token';

// Implement the `StringSchema.jwt()` method.
yup.addMethod(yup.string, 'jwt', function(this: yup.StringSchema, message: string = defaultMessage): yup.StringSchema {
yup.addMethod(yup.string, 'jwt', function(
this: yup.StringSchema,
mode: yup.JwtMode,
maybeKeyOrMessage: string | yup.JwtKey | undefined,
maybeMessage: string | undefined,
): yup.StringSchema {
const checkMessageOrThrow = (value: string | yup.JwtKey | undefined): string => {
if (!(value === undefined || typeof value === 'string')) {
throw new TypeError(
`Invalid argument: 'message' must be a string or undefined but was "${value}"`,
);
}
return value || defaultMessage;
};

const checkKeyOrThrow = (value: string | yup.JwtKey | undefined): yup.JwtKey => {
if (!(typeof value === 'string' || Buffer.isBuffer(value))) {
throw new TypeError(
"Invalid argument: 'key' must be a string, Buffer, or callback function",
);
}
return value;
};

let message: string;
let key: yup.JwtKey;
switch (mode) {
case 'regex':
case 'decode':
message = checkMessageOrThrow(maybeKeyOrMessage);
break;

case 'verify':
key = checkKeyOrThrow(maybeKeyOrMessage);
message = checkMessageOrThrow(maybeMessage);
break;

default:
throw new TypeError(`Invalid argument: 'mode' is `);
}

return this.test({
exclusive: true,
message,
name: 'jwt',
test(this: yup.TestContext, value: string): boolean {
return !!jwt.decode(value);
switch (mode) {
case 'regex':
return /^[0-9a-z\-_]+?\.[0-9a-z\-_]+?\.([0-9a-z\-_]+)?$/i.test(value);

case 'decode':
return !!jwt.decode(value);

case 'verify':
return !!jwt.verify(value, key);

default:
throw new Error('Unreachable code detected');
}
},
});
});
4 changes: 2 additions & 2 deletions test/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { defaultMessage } from '../src/index';

describe('`yup.string().jwt()`', () => {
const nonsenseToken: string = 'foo.bar.qux';
const schema: yup.StringSchema = yup.string().jwt();
const schema: yup.StringSchema = yup.string().jwt('decode');

describe('`isValidSync()`', () => {
const stringPayload: string = 'payload';
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('`yup.string().jwt()`', () => {

it('with a custom message when one is supplied', () => {
const customMessage: string = 'custom message';
const customMessageSchema: yup.StringSchema = yup.string().jwt(customMessage);
const customMessageSchema: yup.StringSchema = yup.string().jwt('decode', customMessage);
expect(() => customMessageSchema.validateSync(nonsenseToken)).toThrowError(customMessage);
});
});
Expand Down

0 comments on commit 5c8187a

Please sign in to comment.