MCPcopy
hub / github.com/jquense/yup

github.com/jquense/yup @v1.0.0 sqlite

repository ↗ · DeepWiki ↗ · release v1.0.0 ↗
348 symbols 823 edges 51 files 5 documented · 1%
README

Yup

Yup is a schema builder for runtime value parsing and validation. Define a schema, transform a value to match, assert the shape of an existing value, or both. Yup schema are extremely expressive and allow modeling complex, interdependent validations, or value transformation.

You are viewing docs for the v1.0.0 of yup, pre-v1 docs are available: here

Killer Features:

  • Concise yet expressive schema interface, equipped to model simple to complex data models
  • Powerful TypeScript support. Infer static types from schema, or ensure schema correctly implement a type
  • Built-in async validation support. Model server-side and client-side validation equally well
  • Extensible: add your own type-safe methods and schema
  • Rich error details, make debugging a breeze

Getting Started

Schema are comprised of parsing actions (transforms) as well as assertions (tests) about the input value. Validate an input value to parse it and run the configured set of assertions. Chain together methods to build a schema.

import { object, string, number, date, InferType } from 'yup';

let userSchema = object({
  name: string().required(),
  age: number().required().positive().integer(),
  email: string().email(),
  website: string().url().nullable(),
  createdOn: date().default(() => new Date()),
});

// parse and assert validity
const user = await userSchema.validate(await fetchUser());

type User = InferType<typeof userSchema>;
/* {
  name: string;
  age: number;
  email?: string | undefined
  website?: string | null | undefined
  createdOn: Date
}*/

Use a schema to coerce or "cast" an input value into the correct type, and optionally transform that value into more concrete and specific values, without making further assertions.

// Attempts to coerce values to the correct type
const parsedUser = userSchema.cast({
  name: 'jimmy',
  age: '24',
  createdOn: '2014-09-23T19:25:25Z',
});
// ✅  { name: 'jimmy', age: 24, createdOn: Date }

Know that your input value is already parsed? You can "strictly" validate an input, and avoid the overhead of running parsing logic.

// ❌  ValidationError "age is not a number"
const parsedUser = await userSchema.validate(
  {
    name: 'jimmy',
    age: '24',
  },
  { strict: true },
);

Table of Contents

Schema basics

Schema definitions, are comprised of parsing "transforms" which manipulate inputs into the desired shape and type, "tests", which make assertions over parsed data. Schema also store a bunch of "metadata", details about the schema itself, which can be used to improve error messages, build tools that dynamically consume schema, or serialize schema into another format.

In order to be maximally flexible yup allows running both parsing and assertions separately to match specific needs

Parsing: Transforms

Each built-in type implements basic type parsing, which comes in handy when parsing serialized data, such as JSON. Additionally types implement type specific transforms that can be enabled.

const num = number().cast('1'); // 1

const obj = object({
  firstName: string().lowercase().trim(),
})
  .camelCase()
  .cast('{"first_name": "jAnE "}'); // { firstName: 'jane' }

Custom transforms can be added

const reversedString = string()
  .transform((currentValue) => currentValue.split('').reverse().join(''))
  .cast('dlrow olleh'); // "hello world"

Transforms form a "pipeline", where the value of a previous transform is piped into the next one. If the end value is undefined yup will apply the schema default if it's configured.

Watch out! values are not guaranteed to be valid types in transform functions. Previous transforms may have failed. For example a number transform may be receive the input value, NaN, or a number.

Validation: Tests

Yup has robust support for assertions, or "tests", over input values. Tests assert that inputs conform to some criteria. Tests are distinct from transforms, in that they do not change or alter the input (or its type) and are usual

Extension points exported contracts — how you extend this code

ArraySchema (Interface)
(no doc) [1 implementers]
src/array.ts
ObjectSchema (Interface)
(no doc) [1 implementers]
src/object.ts
Schema (Interface)
(no doc) [1 implementers]
src/schema.ts
ISchema (Interface)
(no doc) [1 implementers]
src/types.ts
Person (Interface)
(no doc)
test/types/types.ts
MixedOptions (Interface)
(no doc)
src/mixed.ts
DateSchema (Interface)
(no doc)
src/date.ts
LazySpec (Interface)
(no doc)
src/Lazy.ts

Core symbols most depended-on inside this repo

cast
called by 193
src/types.ts
isValid
called by 149
src/Lazy.ts
required
called by 139
src/date.ts
validate
called by 119
src/types.ts
nullable
called by 114
src/date.ts
default
called by 83
src/date.ts
validateSync
called by 57
src/Lazy.ts
concat
called by 51
src/date.ts

Shape

Method 190
Function 95
Interface 34
Class 29

Languages

TypeScript100%

Modules by API surface

src/schema.ts52 symbols
src/object.ts39 symbols
src/string.ts29 symbols
src/array.ts27 symbols
src/number.ts26 symbols
src/date.ts20 symbols
src/Lazy.ts19 symbols
src/tuple.ts18 symbols
src/mixed.ts16 symbols
src/boolean.ts16 symbols
src/types.ts11 symbols
src/Reference.ts10 symbols

Dependencies from manifests, versioned

@4c/cli4.0.4 · 1×
@4c/rollout4.0.2 · 1×
@4c/tsconfig0.4.1 · 1×
@babel/cli7.20.7 · 1×
@babel/core7.20.12 · 1×
@babel/preset-typescript7.18.6 · 1×
@rollup/plugin-babel5.3.1 · 1×
@rollup/plugin-node-resolve13.3.0 · 1×
@types/jest27.5.2 · 1×
@typescript-eslint/eslint-plugin5.50.0 · 1×
@typescript-eslint/parser5.50.0 · 1×
babel-jest27.5.1 · 1×

For agents

$ claude mcp add yup \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact