Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jackkav committed Apr 4, 2024
1 parent 58f8a01 commit f82169c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 68 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { describe, expect, it } from '@jest/globals';

import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../../templating';
import { checkNestedKeys, ensureKeyIsValid } from '../environment-editor';
import { checkNestedKeys, ensureKeyIsValid } from '../environment-utils';

describe('ensureKeyIsValid()', () => {
it.each([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ export const GraphQLEditor: FC<Props> = ({
if (schema) {
graphqlOptions = {
hintOptions: {
schema,
completeSingle: false,
},
infoOptions: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,53 +2,8 @@ import orderedJSON from 'json-order';
import React, { forwardRef, useCallback, useImperativeHandle, useRef, useState } from 'react';

import { JSON_ORDER_PREFIX, JSON_ORDER_SEPARATOR } from '../../../common/constants';
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../templating';
import { CodeEditor, CodeEditorHandle } from '../codemirror/code-editor';

// NeDB field names cannot begin with '$' or contain a period '.'
// Docs: https://github.com/DeNA/nedb#inserting-documents
const INVALID_NEDB_KEY_REGEX = /^\$|\./;

export const ensureKeyIsValid = (key: string, isRoot: boolean): string | null => {
if (key.match(INVALID_NEDB_KEY_REGEX)) {
return `"${key}" cannot begin with '$' or contain a '.'`;
}

if (key === NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME && isRoot) {
return `"${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}" is a reserved key`;
}

return null;
};

/**
* Recursively check nested keys in and immediately return when an invalid key found
*/
export function checkNestedKeys(obj: Record<string, any>, isRoot = true): string | null {
for (const key in obj) {
let result: string | null = null;

// Check current key
result = ensureKeyIsValid(key, isRoot);

// Exit if necessary
if (result) {
return result;
}

// Check nested keys
if (typeof obj[key] === 'object') {
result = checkNestedKeys(obj[key], false);
}

// Exit if necessary
if (result) {
return result;
}
}

return null;
}
import { checkNestedKeys } from './environment-utils';

export interface EnvironmentInfo {
object: Record<string, any>;
Expand Down
46 changes: 46 additions & 0 deletions packages/insomnia/src/ui/components/editors/environment-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME } from '../../../templating';

// NeDB field names cannot begin with '$' or contain a period '.'
// Docs: https://github.com/DeNA/nedb#inserting-documents
const INVALID_NEDB_KEY_REGEX = /^\$|\./;

export const ensureKeyIsValid = (key: string, isRoot: boolean): string | null => {
if (key.match(INVALID_NEDB_KEY_REGEX)) {
return `"${key}" cannot begin with '$' or contain a '.'`;
}

if (key === NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME && isRoot) {
return `"${NUNJUCKS_TEMPLATE_GLOBAL_PROPERTY_NAME}" is a reserved key`;
}

return null;
};

/**
* Recursively check nested keys in and immediately return when an invalid key found
*/
export function checkNestedKeys(obj: Record<string, any>, isRoot = true): string | null {
for (const key in obj) {
let result: string | null = null;

// Check current key
result = ensureKeyIsValid(key, isRoot);

// Exit if necessary
if (result) {
return result;
}

// Check nested keys
if (typeof obj[key] === 'object') {
result = checkNestedKeys(obj[key], false);
}

// Exit if necessary
if (result) {
return result;
}
}

return null;
}

0 comments on commit f82169c

Please sign in to comment.