@pcvl/schema / InterpretedSchema
Abstract Class: InterpretedSchema<Input, Output>
Defined in: schemas/interpretedSchema.ts:32
The class every schema a pvl.* factory builds extends: a Schema that validates by running its modifiers around its type check, in the order they were chained. A compiled schema validates the same values without it, so a field, .transform() and pvl.compile() take and hand back a Schema instead.
Example
import { pvl, type InterpretedSchema, type Schema } from '@pcvl/schema';
const name: InterpretedSchema<string> = pvl.string().min(2);
const field: Schema<string> = name;
field.validate('A'); // { issues: [...] } — TOO_SMALLExtended by
Type Parameters
Input
Input = unknown
Output
Output = Input
Implements
Schema<Input,Output>
Methods
standalone()
standalone(): InterpretedSchema<Input, Output> & StandaloneMarker;Defined in: schemas/interpretedSchema.ts:119
Marks this schema as a Standalone Key: as a field of pvl.object(), it is kept in that object's shape, so it can be validated on its own, and once the object is compiled, @pcvl/compiler gives it a compiled validator of its own. Validation is unchanged.
The schema comes back as the same type it went in as, marked, so the chain goes on: modifiers can follow it, and an object keeps its shape and an array its element. It is valid on any schema, a field or not, and calling it again changes nothing.
Returns
InterpretedSchema<Input, Output> & StandaloneMarker
Example
import { pvl } from '@pcvl/schema';
const user = pvl.object({
email: pvl.string().standalone().min(3),
age: pvl.number().int(),
});
user.shape.email.validate('a@b.c'); // { value: 'a@b.c' }
// user.shape.age — a compile error: `age` isn't a Standalone KeyImplementation of
validate()
validate(value): Result<Output>;Defined in: schemas/interpretedSchema.ts:89
Checks a value against this schema and returns a Result synchronously. It never throws for an invalid value — a rejection comes back as issues, so the two branches are handled the same way every time.
Parameters
value
unknown
Returns
Result<Output>
Example
import { pvl } from '@pcvl/schema';
const result = pvl.number().int().validate(1.5);
if (result.issues) {
console.log(result.issues[0]?.message); // 'Number must be an integer'
} else {
console.log(result.value);
}