node-ottoman icon indicating copy to clipboard operation
node-ottoman copied to clipboard

StringType enum option allows passing "undefined" and undefined

Open PeterD1524 opened this issue 9 months ago • 0 comments

ottoman version: 2.5.2

StringType enum option allows passing "undefined" and undefined.

It also sets the value to "undefined".

When constructing the document, undefined will be cast to "undefined".

When saving the document, "undefined" will pass the check here:

https://github.com/couchbaselabs/node-ottoman/blob/f205b2c60146abe04165e78659c16ce0d15a4164/src/schema/types/string-type.ts#L262

example:

import { connect, model, Schema } from "ottoman";

await connect({
  bucketName: "travel-sample",
  connectionString: "couchbase://localhost",
  username: "Administrator",
  password: "password",
});

const userSchema = new Schema({
  gender: { type: String, enum: ["M", "F"] },
});

const User = model("User", userSchema, {
  scopeName: "_default",
  collectionName: "_default",
});

const a = new User({ gender: undefined });
await a.save();
console.log(a.gender === "undefined"); // got true, expected false

const aa = await User.findById(a.id);
console.log(aa.gender === "undefined"); // got true, expected false

const b = new User({ gender: "undefined" });
await b.save(); // passes, expected ValidationError
console.log(b.gender === "undefined"); // got true, expected false

const bb = await User.findById(b.id);
console.log(bb.gender === "undefined"); // got true, expected false

PeterD1524 avatar May 25 '25 14:05 PeterD1524