Skip to content

Commit

Permalink
fix(validator): 🐛 implement proper fix for 85618aa
Browse files Browse the repository at this point in the history
  • Loading branch information
JamesNZL committed Jul 17, 2022
1 parent cf053b4 commit efaed22
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 13 deletions.
10 changes: 5 additions & 5 deletions src/options/PropertySelects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { NotionClient } from '../apis/notion';
import { Storage } from '../apis/storage';

import { SavedFields } from '.';
import { InputFieldValidator, ValidatorConstructor } from './validator';
import { ValidatorConstructor, typeGuards } from './validator';
import { CONFIGURATION, SupportedTypes } from './configuration';

import { Select } from '../elements';
Expand Down Expand Up @@ -74,16 +74,16 @@ export class SelectPropertyValueSelect extends PropertySelect {
type: PropertySelect['type'],
Validator?: ValidatorConstructor;
fieldKey: PropertySelect['fieldKey'],
getDatabaseId: () => Promise<SupportedTypes | typeof InputFieldValidator.INVALID_INPUT>,
getDatabaseId: () => SupportedTypes,
propertySelect: PropertySelect;
}) {
super({ id, type, Validator, fieldKey });

this.propertySelect = propertySelect;

this.propertySelect.addEventListener('input', async () => {
const databaseId = await getDatabaseId();
if (typeof databaseId !== 'string') return;
const databaseId = getDatabaseId();
if (!typeGuards.isUUIDv4(databaseId)) return console.log('bye!', { databaseId });

const accessToken = (await Storage.getNotionAuthorisation()).accessToken ?? await CONFIGURATION.FIELDS['notion.accessToken'].input.validate(true);

Expand All @@ -100,7 +100,7 @@ export class SelectPropertyValueSelect extends PropertySelect {
type: PropertySelect['type'],
Validator?: ValidatorConstructor;
fieldKey: PropertySelect['fieldKey'],
getDatabaseId: () => Promise<SupportedTypes | typeof InputFieldValidator.INVALID_INPUT>,
getDatabaseId: () => SupportedTypes,
propertySelect: PropertySelect;
}): SelectPropertyValueSelect {
if (!(SelectPropertyValueSelect.instances.get(id) instanceof SelectPropertyValueSelect)) {
Expand Down
6 changes: 3 additions & 3 deletions src/options/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Storage } from '../apis/storage';
import { OAuth2 } from '../apis/oauth';

import { SavedFields } from './';
import { EmojiField, InputFieldValidator, RequiredNotionDatabaseIdField, RequiredStringField, StringField } from './validator';
import { EmojiField, InputFieldValidator, RequiredNotionDatabaseIdField, RequiredStringField, StringField, typeGuards } from './validator';
import { CONFIGURATION, SupportedTypes } from './configuration';

import { Element, Button, Select, KeyValueGroup } from '../elements';
Expand Down Expand Up @@ -189,7 +189,7 @@ const DatabaseSelect = <const>{
type: 'select',
Validator: StringField,
fieldKey: 'notion.propertyValues.categoryCanvas',
getDatabaseId: DatabaseSelect.element.validate.bind(DatabaseSelect.element),
getDatabaseId: DatabaseSelect.element.getValue.bind(DatabaseSelect.element),
propertySelect: DatabaseSelect.propertySelects.category,
});
},
Expand Down Expand Up @@ -456,7 +456,7 @@ Object.values(buttons.restore).forEach(button => button.addEventListener('click'

DatabaseSelect.element.addEventListener('input', async () => {
const databaseId = DatabaseSelect.element.getValue();
if (typeof databaseId !== 'string') return;
if (!typeGuards.isUUIDv4(databaseId)) return;

const accessToken = (await Storage.getNotionAuthorisation()).accessToken ?? await CONFIGURATION.FIELDS['notion.accessToken'].input.validate(true);

Expand Down
10 changes: 5 additions & 5 deletions src/options/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,17 +300,17 @@ const typeGuardModifiers = <const>{
},
};

const typeGuards = <const>{
isString(value: unknown) {
export const typeGuards = <const>{
isString(value: unknown): value is string {
return (typeof value === 'string');
},
isParsableNumber(value: unknown) {
isParsableNumber(value: unknown): value is string {
return (typeof value === 'string' && !isNaN(Number(value)));
},
isEmojiRequest(value: unknown) {
isEmojiRequest(value: unknown): value is string {
return (typeof value === 'string' && (<string[]>VALID_EMOJIS).includes(value));
},
isUUIDv4(value: unknown) {
isUUIDv4(value: unknown): value is string {
// allow hyphens to be optional as the Notion API doesn't require them
// also, Notion URLs don't have them, so it wouldn't be very user friendly to require them
const hyphensRegex = /^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i;
Expand Down

0 comments on commit efaed22

Please sign in to comment.