Skip to content

StringType enum option allows passing "undefined" and undefined #801

@PeterD1524

Description

@PeterD1524

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:

if (value !== 'undefined' && typeof this.enumValues !== 'undefined') {

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions