Skip to content

Commit

Permalink
feat: added validation for Open AI key
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniyal-Qureshi committed Jun 2, 2024
1 parent 21f33b6 commit c1b39d4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Validator | Description
**isAlpha(str [, locale, options])** | check if the string contains only letters (a-zA-Z).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bg-BG', 'bn', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP', 'ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']` and defaults to `en-US`. Locale list is `validator.isAlphaLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAlphanumeric(str [, locale, options])** | check if the string contains only letters and numbers (a-zA-Z0-9).<br/><br/>`locale` is one of `['ar', 'ar-AE', 'ar-BH', 'ar-DZ', 'ar-EG', 'ar-IQ', 'ar-JO', 'ar-KW', 'ar-LB', 'ar-LY', 'ar-MA', 'ar-QA', 'ar-QM', 'ar-SA', 'ar-SD', 'ar-SY', 'ar-TN', 'ar-YE', 'bn', 'bg-BG', 'cs-CZ', 'da-DK', 'de-DE', 'el-GR', 'en-AU', 'en-GB', 'en-HK', 'en-IN', 'en-NZ', 'en-US', 'en-ZA', 'en-ZM', 'eo', 'es-ES', 'fa-IR', 'fi-FI', 'fr-CA', 'fr-FR', 'he', 'hi-IN', 'hu-HU', 'it-IT', 'kk-KZ', 'ko-KR', 'ja-JP','ku-IQ', 'nb-NO', 'nl-NL', 'nn-NO', 'pl-PL', 'pt-BR', 'pt-PT', 'ru-RU', 'si-LK', 'sl-SI', 'sk-SK', 'sr-RS', 'sr-RS@latin', 'sv-SE', 'th-TH', 'tr-TR', 'uk-UA']`) and defaults to `en-US`. Locale list is `validator.isAlphanumericLocales`. `options` is an optional object that can be supplied with the following key(s): `ignore` which can either be a String or RegExp of characters to be ignored e.g. " -" will ignore spaces and -'s.
**isAscii(str)** | check if the string contains ASCII chars only.
**isOpenAIKey(str)** | check if the string is valid OpenAI Key
**isBase32(str [, options])** | check if the string is base32 encoded. `options` is optional and defaults to `{ crockford: false }`.<br/> When `crockford` is true it tests the given base32 encoded string using [Crockford's base32 alternative][Crockford Base32].
**isBase58(str)** | check if the string is base58 encoded.
**isBase64(str [, options])** | check if the string is base64 encoded. `options` is optional and defaults to `{ urlSafe: false }`<br/> when `urlSafe` is true it tests the given base64 encoded string is [url safe][Base64 URL Safe].
Expand Down
5 changes: 4 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,9 @@ import isStrongPassword from './lib/isStrongPassword';

import isVAT from './lib/isVAT';

const version = '13.12.0';
import isOpenAIKey from './lib/isOpenAIKey';

const version = '13.11.0';

const validator = {
version,
Expand Down Expand Up @@ -240,6 +242,7 @@ const validator = {
isLicensePlate,
isVAT,
ibanLocales,
isOpenAIKey,
};

export default validator;
9 changes: 9 additions & 0 deletions src/lib/isOpenAIKey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import assertString from './util/assertString';

const openAIKey = /^sk-[A-Za-z0-9]{48}$/;


export default function isOpenAIKey(str) {
assertString(str);
return openAIKey.test(str);
}
22 changes: 22 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -6410,6 +6410,28 @@ describe('Validators', () => {
});
});


it('should validate OpenAI API keys', () => {
test({
validator: 'isOpenAIKey',
valid: [
'sk-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGhIjKl',
'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',
'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',
'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',
'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',
],
invalid: [
'sk-ABCDEFGHIJKLMNOPQRSTUVWXYZ123456', // Not enough variety of characters
'api-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGhIjKlMnOp', // Incorrect prefix
'sk-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789', // Fewer characters
'sk-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789AbCdEfGhIjKlMnOpQrStUvWxYz', // More characters
'sk-AbCdEfGhIjKlMnOpQrStUvWxYz0123456789@#%^&*()', // Special characters
'', // Empty string
],
});
});

it('should validate full-width strings', () => {
test({
validator: 'isFullWidth',
Expand Down

0 comments on commit c1b39d4

Please sign in to comment.