convex
Version:
Client for the Convex Cloud
95 lines (94 loc) • 2.04 kB
JavaScript
;
import { convexToJson } from "./value.js";
export class Validator {
/**
* @internal
*/
constructor(json, optional) {
this.json = json;
this.optional = optional;
}
}
export const v = {
id(tableName) {
return new Validator({ type: "id", tableName }, false);
},
null() {
return new Validator({ type: "null" }, false);
},
/**
* Alias for `v.float64()`
*/
number() {
return new Validator({ type: "number" }, false);
},
float64() {
return new Validator({ type: "number" }, false);
},
/**
* @deprecated Use `v.int64()` instead
*/
bigint() {
return new Validator({ type: "bigint" }, false);
},
int64() {
return new Validator({ type: "bigint" }, false);
},
boolean() {
return new Validator({ type: "boolean" }, false);
},
string() {
return new Validator({ type: "string" }, false);
},
bytes() {
return new Validator({ type: "bytes" }, false);
},
literal(literal) {
const value = convexToJson(literal);
return new Validator({ type: "literal", value }, false);
},
array(values) {
return new Validator({ type: "array", value: values.json }, false);
},
object(schema) {
return new Validator(
{
type: "object",
value: Object.fromEntries(
Object.entries(schema).map(([k, v2]) => [
k,
{ fieldType: v2.json, optional: v2.optional }
])
)
},
false
);
},
/** @internal */
record(keys, values) {
return new Validator(
{
type: "record",
keys: keys.json,
values: { fieldType: values.json, optional: values.optional }
},
false
);
},
union(...schemaTypes) {
return new Validator(
{
type: "union",
value: schemaTypes.map((t) => t.json)
},
false
);
},
any() {
return new Validator({ type: "any" }, false);
},
optional(inner) {
return new Validator(inner.json, true);
}
};
//# sourceMappingURL=validator.js.map