Betterlol/moon_zod/core does not have a README file
pub(open) trait NumericBound {
fn to_bound(Self) -> Double
}impl NumericBound for Intfn to_bound(self : Int) -> Doubleimpl NumericBound for Int64fn to_bound(self : Int64) -> Doubleimpl NumericBound for Doublefn to_bound(self : Double) -> Doublepub(all) enum Constraint {
Minimum(String, Double, Bool)
Maximum(String, Double, Bool)
ExactLength(String, Int)
Multiple(Double)
Integer
Format(String)
Pattern(String)
Prefix(String)
Suffix(String)
Contains(String)
Finite
SafeInteger
Opaque
} derive(Debug)#deprecated("`Constraint::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn Constraint::to_repr(Constraint) -> Reprpub(all) struct ConstraintInfo {
min_value : Double?
max_value : Double?
min_inclusive : Bool
max_inclusive : Bool
format : String
pattern : String
patterns : Array[String]
formats : Array[String]
is_int : Bool
is_positive : Bool
is_negative : Bool
multiple_of : Double
multiples : Array[Double]
custom_messages : Array[String]
}pub(all) enum IssueCode {
InvalidType(String)
TooBig(String, Double, Bool)
TooSmall(String, Double, Bool)
InvalidFormat(String)
NotMultipleOf(Double)
UnrecognizedKeys(Array[String])
InvalidUnion(Array[Array[RawIssue]])
MissingRequired(String)
InvalidKey(String)
InvalidElement(String, Int)
InvalidValue(Array[Json])
Custom
} derive(Eq, Debug)#deprecated("`ObjectMode::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ObjectMode::to_repr(ObjectMode) -> Repr#deprecated("`PathSegment::equal` is deprecated, use `Eq::equal` instead.")
fn PathSegment::equal(PathSegment, PathSegment) -> Bool#deprecated("`PathSegment::not_equal` is deprecated, use `Eq::not_equal` instead.")
fn PathSegment::not_equal(x : PathSegment, y : PathSegment) -> Bool#deprecated("`PathSegment::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn PathSegment::to_repr(PathSegment) -> Reprpub(all) struct Rule {
constraint : Constraint
explicit_message : Bool
check : (Json) -> Bool
message : String
code : IssueCode
annotation : Json
} derive(Debug)pub(all) struct Schema {
id : Int
schema_type : SchemaType
rules : Array[Rule]
description : String
required_error : String
invalid_type_error : String
name : String
brand : String
} derive(Debug)let colors = enum_values(["red", "green", "blue"])
let no_green = colors.exclude(["green"])
// now accepts only "red" and "blue"let colors = enum_values(["red", "green", "blue"])
let warm = colors.extract(["red", "orange"]) // "orange" ignored, only "red" kept
// now accepts only "red"string().min(2).message("姓名至少需要 2 个字符")let s = object({"a": string(), "b": number()}).omit(["b"])
s.parse(json) // validates only field "a"fn Schema::parse_stream(self : Schema, path? : String, max_length? : Int, max_depth? : Int) -> StreamParserlet s = object({"name": string(), "age": number()}).partial()
s.parse(json) // all fields are now optionallet s = object({"a": string(), "b": number()}).pick(["a"])
s.parse(json) // validates only field "a"let s = string()
.transform(fn(s) { Ok(Json::string(s.length().to_string())) })
.pipe(number().min(5))
// string check → transform → number check(.min(5))fn Schema::safe_parse(self : Schema, json : Json, params : ParseParams) -> Result[Json, Array[ValidationError]]schema.safe_parse(json, { error_map: fn(code, path, input) {
match code { InvalidType(expected) => Some("必须是: " + expected) _ => None }
}})let s = string().transform(fn(json) {
match json {
String(s) => Ok(Json::string(s + "!"))
_ => Err("expected string")
}
})pub(all) enum SchemaType {
StringType
NumberType
BooleanType
NullType
AnyType
UnknownType
ObjectType(Map[String, Schema], ObjectMode)
ArrayType(Schema)
TupleType(Array[Schema])
OptionalType(Schema)
NullableType(Schema)
PrefaultType(Schema, Json)
DefaultType(Schema, Json)
EnumType(Array[String])
UnionType(Array[Schema])
IntersectionType(Array[Schema])
PreprocessType(TransformClosure, Schema)
TransformType(Schema, TransformClosure)
LiteralType(Json)
LazyType(() -> Schema)
DiscriminatedUnionType(String, Map[String, Schema])
PipeType(Schema, TransformClosure, Schema)
} derive(Debug)#deprecated("`SchemaType::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn SchemaType::to_repr(SchemaType) -> Reprpub(all) enum StreamParseResult {
StreamPending
StreamValid(Json)
StreamInvalidJson(String)
StreamInvalidSchema(Array[ValidationError])
}pub struct StreamParser {
// private fields
}pub(all) struct TransformClosure {
f : (Json) -> Result[Json, String]
kind : TransformKind
} derive(Debug)#deprecated("`TransformClosure::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn TransformClosure::to_repr(TransformClosure) -> Reprpub(all) enum TransformKind {
CustomTransform
IdentityTransform
TrimTransform
LowerTransform
UpperTransform
} derive(Debug)#deprecated("`TransformKind::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn TransformKind::to_repr(TransformKind) -> Reprpub(all) struct ValidationError {
code : IssueCode
path : String
path_segments : Array[PathSegment]
message : String
got : Json
} derive(Debug)#deprecated("`ValidationError::to_repr` is deprecated, use `@moonbitlang/core/debug.Debug::to_repr` instead.")
fn ValidationError::to_repr(ValidationError) -> Reprlet pet = @moon_zod.discriminated_union("type", {
"dog": @moon_zod.object({ "bark": @moon_zod.boolean() }),
"cat": @moon_zod.object({ "meow": @moon_zod.boolean() }),
})fn escape_function_name(name : String) -> Stringfn escape_ident(name : String) -> Stringfn escape_mbt_string(s : String) -> Stringfn escape_type_name(name : String) -> Stringfn escape_variable_name(name : String) -> Stringfn format_double_simple(v : Double) -> Stringfn is_keyword(name : String) -> Boollet schema = @moon_zod.literal(Json::string("hello"))
schema.parse(Json::string("hello")) // Ok
schema.parse(Json::string("world")) // Errfn tree_schema() -> @moon_zod.Schema {
@moon_zod.recursive(fn() {
@moon_zod.object({
"value": @moon_zod.number(),
"children": @moon_zod.array(@moon_zod.recursive(tree_schema)).optional(),
})
})
}Install
Download zipA runtime JSON schema validation library for MoonBit, inspired by Zod and Pydantic