Skip to content
This repository was archived by the owner on Jun 8, 2023. It is now read-only.

Commit 0bd3257

Browse files
authored
Merge pull request francescov1#100 from francescov1/handle-special-character-keys
handle special character keys
2 parents 6230f57 + a541892 commit 0bd3257

File tree

5 files changed

+12
-5
lines changed

5 files changed

+12
-5
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "mongoose-tsgen",
33
"description": "A Typescript interface generator for Mongoose that works out of the box.",
4-
"version": "9.0.7",
4+
"version": "9.0.8",
55
"author": "Francesco Virga @francescov1",
66
"bin": {
77
"mtgen": "./bin/run"

src/helpers/parser.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ const formatKeyEntry = ({
3030
let line = "";
3131

3232
if (key) {
33-
line += key;
33+
// If the key contains any special characters, we need to wrap it in quotes
34+
line += /^\w*$/.test(key) ? key : JSON.stringify(key);
35+
3436
if (isOptional) line += "?";
3537
line += ": ";
3638
}

src/helpers/tests/artifacts/user.gen.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ otherNumberString: number;
8080
otherStringString: string;
8181
enumWithNull?: "a" | "b" | "c" | null;
8282
enumWithoutNull?: "a" | "b" | "c";
83+
"special-character"?: string;
8384
_id: mongoose.Types.ObjectId;
8485
name: string;
8586
}
@@ -208,6 +209,7 @@ otherNumberString: number;
208209
otherStringString: string;
209210
enumWithNull?: "a" | "b" | "c" | null;
210211
enumWithoutNull?: "a" | "b" | "c";
212+
"special-character"?: string;
211213
_id: mongoose.Types.ObjectId;
212214
name: string;
213215
}

src/helpers/tests/artifacts/user.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import mongoose, { Schema } from 'mongoose';
2-
import { UserDocument, UserModel, UserSchema, UserQueries, UserObject } from "./user.gen";
2+
import { UserDocument, UserModel, UserSchema, UserObject } from "./user.gen";
33

44
// UserSchema type
55
const UserSchema: UserSchema = new Schema({
@@ -102,6 +102,9 @@ const UserSchema: UserSchema = new Schema({
102102
enumWithoutNull: {
103103
type: String,
104104
enum: ["a", "b", "c"]
105+
},
106+
"special-character": {
107+
type: String
105108
}
106109
}, {
107110
toObject: {

0 commit comments

Comments
 (0)