#Mongo schema validation !

5 messages · Page 1 of 1 (latest)

clever ferry
#

@Schema()
export class Category {

@Prop({unique:true})
category: string;
@Prop()
description:string;
@Prop()
featured:boolean;
@Prop({default:true})
status:boolean
@Prop()
subcategory:[]

}

As a newbie in nest, could anyone help me how to validate array uniquely in this case subcategory prop above . ????

outer sluiceBOT
#

Please format your question or answer with Markdown formatting.
It leads to better readability and an easier time to spot problems.
For code blocks, you can wrap your block with three back ticks before and after the block, and after the first three back ticks you can add a language (like ts) to add syntax highlighting.
e.g.

```ts
@Injectable()
export class MySuperAwesomeService {
constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

getRandomNumber(): number {
return Math.round(Math.random() * 1000);
}
}
```

Becomes :point_down:

@Injectable()
export class MySuperAwesomeService {
  constructor(@Inject('InjectionToken') private readonly dep: SomeDependency) {}

  getRandomNumber(): number {
    return Math.round(Math.random() * 1000);
  }
}
mossy prairie
#

What do you mean by "array uniquely"? Do you mean put a unique index on the stored array? Or enter an array, where all the items are unique?

clever ferry
#

i want , all the items are unique in subcategory field of the category schema ?

#

with primitive types i usually do like @Prop({unique:true}) category:string