Skip to content

Commit

Permalink
Escape ' and \ in generated string literals
Browse files Browse the repository at this point in the history
  • Loading branch information
mkubliniak committed Apr 7, 2021
1 parent 3dc670f commit e95ae4c
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/__tests__/basic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ describe('some basic tests', () => {
propertyName1: Joi.boolean().required(),
dateCreated: Joi.date(),
count: Joi.number(),
obj: Joi.object()
obj: Joi.object(),
escape: Joi.string().valid("a'b", 'c"d', "e'f'g", 'h"i"j', '\\\\').required()
})
.label('TestSchema')
.description('a test schema definition');
Expand All @@ -23,6 +24,7 @@ describe('some basic tests', () => {
export interface TestSchema {
count?: number;
dateCreated?: Date;
escape: 'a\\'b' | 'c"d' | 'e\\'f\\'g' | 'h"i"j' | '\\\\\\\\';
/**
* Test Schema Name
*/
Expand Down
4 changes: 3 additions & 1 deletion src/__tests__/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ describe('enums tests', () => {
test('enums using valid()', () => {
const schema = Joi.object({
topColour: Joi.string().valid('red', 'green', 'orange', 'blue').required(),
bottomColour: Joi.string().valid('red', 'green', 'orange', 'blue').required()
bottomColour: Joi.string().valid('red', 'green', 'orange', 'blue').required(),
escape: Joi.string().valid("a'b", 'c"d', "e'f'g", 'h"i"j', '\\\\').required()
})
.label('TestSchema')
.description('a test schema definition');
Expand All @@ -19,6 +20,7 @@ describe('enums tests', () => {
export interface TestSchema {
topColour: 'red' | 'green' | 'orange' | 'blue';
bottomColour: 'red' | 'green' | 'orange' | 'blue';
escape: 'a\\'b' | 'c"d' | 'e\\'f\\'g' | 'h"i"j' | '\\\\\\\\';
}`);
});

Expand Down
6 changes: 3 additions & 3 deletions src/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Joi from 'joi';
import { filterMap } from './utils';
import { filterMap, toStringLiteral } from './utils';
import { TypeContent, makeTypeContentRoot, makeTypeContentChild, Settings, JsDoc } from './types';

// see __tests__/joiTypes.ts for more information
Expand Down Expand Up @@ -249,7 +249,7 @@ function parseBasicSchema(details: BasicDescribe, settings: Settings): TypeConte
// at least one value
if (values && values.length !== 0) {
const allowedValues = values.map((value: unknown) =>
makeTypeContentChild({ content: typeof value === 'string' ? `'${value}'` : `${value}` })
makeTypeContentChild({ content: typeof value === 'string' ? toStringLiteral(value) : `${value}` })
);

if (values[0] === null) {
Expand All @@ -274,7 +274,7 @@ function parseStringSchema(details: StringDescribe, settings: Settings): TypeCon
const allowedValues = values.map(value =>
stringAllowValues.includes(value) && value !== ''
? makeTypeContentChild({ content: `${value}` })
: makeTypeContentChild({ content: `'${value}'` })
: makeTypeContentChild({ content: toStringLiteral(value) })
);

if (values.filter(value => stringAllowValues.includes(value)).length == values.length) {
Expand Down
8 changes: 8 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ export function filterMap<T, K>(list: T[], mapper: (t: T) => K | undefined): K[]
return res;
}, []);
}

/**
* Escape value so that it can be go into single quoted string literal.
* @param value
*/
export function toStringLiteral(value: string): string {
return `'${value.replace(/\\/g, '\\\\').replace(/'/g, "\\'")}'`;
}

0 comments on commit e95ae4c

Please sign in to comment.