{"library":"schema-to-yup","title":"Schema to Yup Schema Builder","description":"schema-to-yup is a utility library for generating Yup validation schemas directly from declarative schema definitions such as JSON Schema or GraphQL type definitions. It helps automate the creation of validation logic, reducing boilerplate, especially in applications that consume or define data models externally. Currently at version 1.12.18, the package appears to be actively maintained, indicated by regular minor releases. A key differentiator is its configurability, allowing developers to customize error messages, enforce default `required` behavior, and integrate with existing schema ecosystems. While it supports common JSON Schema features, users should be aware that it does not automatically resolve `$ref` (JSON Pointers), requiring a pre-processing step with another library if `$ref`s are present in the input schema.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install schema-to-yup"],"cli":null},"imports":["import { buildYup } from 'schema-to-yup';","import type { YupBuilderConfig } from 'schema-to-yup';","import type { TypeHandlerConfig } from 'schema-to-yup';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { buildYup } from 'schema-to-yup';\nimport * as yup from 'yup';\n\nconst jsonSchema = {\n  $schema: \"http://json-schema.org/draft-07/schema#\",\n  $id: \"http://example.com/person.schema.json\",\n  title: \"Person\",\n  description: \"A person\",\n  type: \"object\",\n  properties: {\n    name: {\n      description: \"Name of the person\",\n      type: \"string\"\n    },\n    email: {\n      type: \"string\",\n      format: \"email\"\n    },\n    age: {\n      description: \"Age of person\",\n      type: \"number\",\n      exclusiveMinimum: 0\n    }\n  },\n  required: [\"name\", \"email\"]\n};\n\nconst config = {\n  errMessages: {\n    age: {\n      required: \"A person must have an age\",\n      exclusiveMinimum: \"Age must be greater than zero\"\n    },\n    email: {\n      required: \"You must enter an email address\",\n      format: \"Not a valid email address\"\n    }\n  }\n};\n\nconst yupSchema = buildYup(jsonSchema, config);\n\nasync function validateExample() {\n  // Example of valid data\n  const validData = { name: \"Jane Doe\", email: \"jane@example.com\", age: 30 };\n  const isValid1 = await yupSchema.isValid(validData);\n  console.log('Valid data result:', isValid1); // Expected: true\n\n  // Example of invalid data (missing required field, invalid email format)\n  const invalidData = { name: \"John\", email: \"invalid-email\" };\n  try {\n    await yupSchema.validate(invalidData, { abortEarly: false });\n  } catch (error) {\n    if (error instanceof yup.ValidationError) {\n      console.log('Invalid data errors:', error.errors);\n    } else {\n      console.error('Validation unexpected error:', error);\n    }\n  }\n}\n\nvalidateExample();","lang":"typescript","description":"This quickstart demonstrates how to define a JSON schema, configure custom error messages, build a Yup schema using `buildYup`, and then use the generated schema to validate data, showing both valid and invalid scenarios.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}