npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

yup-field-props-base

v1.0.0

Published

Get the validation properties for a field in a yup schema

Readme

Yup Field Props Base

Yup Field Props Base is a library designed to simplify extracting validation properties from a Yup schema field based on current form state. This allows your schema to serve as the single source of truth for assistive form UIs. It's primarily intended to be used with yup-field-props-react rather than as a standalone library.

Installation

Install the library using npm or yarn:

npm install yup-field-props-base
# or
yarn add yup-field-props-base

getFieldProps Function

The getFieldProps function generates field properties for a specified field name based on a Yup schema, making form field validation management more straightforward.

Description

This utility function takes a field name, a Yup schema, and additional parameters to generate properties for the specified field. By analyzing the schema, it determines validation rules and other field properties, enabling consistent form field management.

Parameters

  • name (string): The name of the field for which to generate properties
  • schema (ObjectSchema): The Yup schema defining validation rules
  • values (any): Current values of all form fields
  • context (AnyObject, optional): Additional context for validation
  • throwError (boolean, optional): Whether to throw errors (true) or return default field properties (false, default) on failure

Return Value

Returns an object containing the properties for the specified field, including validation rules and other relevant information derived from the Yup schema.

Usage

Here's how to use the getFieldProps function:

  1. Import the necessary modules:
import { getFieldProps } from 'yup-field-props-base'
import * as yup from 'yup'
import type { NumberFieldProps, StringFieldProps } from 'yup-field-props-base'
  1. Define your Yup schema with validation rules:
const schema = yup.object().shape({
  name: yup.string().required('Name is required'),
  age: yup
    .number()
    .required('Age is required')
    .min(13, 'You are not old enough')
    .max(120, 'This is too old'),
  email: yup
    .string()
    .email('Invalid email format')
    .required('Email is required'),
  username: yup
    .string()
    .oneOf([yup.ref('name'), yup.ref('email')], 'Must match name or email'),
  phoneNumber: yup.string().when('age', ([age], schema) => {
    return age >= 18
      ? schema.required('Phone number required for adults')
      : schema
  }),
})
  1. Set the values for your form fields:
const values = {
  name: 'Kevin',
  age: 20,
  email: '[email protected]',
  username: '[email protected]',
  phoneNumber: '(123)-456-7890',
}
  1. Generate the field properties using the getFieldProps function:
// Get properties for a number field
const ageProps = getFieldProps<NumberFieldProps>({
  name: 'age',
  schema,
  values: { name: '', age: '' }, // Empty values to check validation
  context: {},
})

// Get properties for a string field with references
const usernameProps = getFieldProps<StringFieldProps>({
  name: 'username',
  schema,
  values,
  context: {},
})

// Get properties for a field with conditional validation
const phoneNumberProps = getFieldProps<StringFieldProps>({
  name: 'phoneNumber',
  schema,
  values,
  context: {},
})
  1. Use the generated field properties:
console.log(ageProps)
console.log(usernameProps)
console.log(phoneNumberProps)

Example output:

{
  type: 'number',
  required: true,
  nullable: false,
  oneOf: [],
  notOneOf: [],
  tests: [
    { name: 'min', params: { min: 13 } },
    { name: 'max', params: { max: 120 } }
  ],
  min: 13,
  max: 120
}

{
  type: 'string',
  required: false,
  nullable: false,
  oneOf: ['Kevin', '[email protected]'],
  notOneOf: [],
  tests: []
}

{
  type: 'string',
  required: true,
  nullable: false,
  oneOf: [],
  notOneOf: [],
  tests: [{ name: 'required', params: undefined }]
}