juniper
Version:
ESM JSON Schema builder for static Typescript inference.
46 lines (45 loc) • 1.03 kB
JavaScript
import { AbstractSchema } from '../lib/schema.js';
export class EnumSchema extends AbstractSchema {
#enum;
constructor(options = {}){
super(options);
this.#enum = options.enum ?? [];
}
static create(options) {
return new EnumSchema(options);
}
enum(val) {
return this.enums([
val
]);
}
enums(enums) {
return this.clone({
enum: [
...this.#enum,
...enums
]
});
}
getCloneParams() {
return {
...super.getCloneParams(),
enum: [
...this.#enum
]
};
}
toSchema(params) {
const base = super.toSchema(params);
const enums = [
...new Set(this.#enum)
];
if (enums.length === 1 && !params.openApi30) {
[base.const] = enums;
} else {
base.enum = enums;
}
return base;
}
}
//# sourceMappingURL=enum.js.map