hapijs / hapijs/joi

Asterisk on field label depending on schema

Open
#2,560 5 comments 0 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
21.2k
Forks
1.5k
Avg merge
4h 57m
Merged PRs (30d)
14

Description

#### Support plan

* *is this issue currently blocking your project?* (yes/no): Yes
* *is this issue affecting a production system?* (yes/no): No

#### Context

* *node version*: 14.15.5
* *module version*: 17.4.0
* *environment* (e.g. node, browser, native): browser
* *used with* (e.g. hapi application, another framework, standalone, ...): React hook form
* *any other relevant information*: No

#### How can we help?

I would like to display a star (or asterisk) on my fields label based on the schema, I was able to do it with Yup (following code), but I can't figure out anything that would do it with Joi. It should be able to change on `when` conditions triggers also.

```js
const fieldValidationSchema = schema ? reach(schema, fieldName) : false;

// useWatch triggers render on form field(s) change
const form = useWatch({
// eslint-disable-next-line no-underscore-dangle
name: fieldValidationSchema?._deps?.length > 0 ? [fieldName, ...fieldValidationSchema._deps] : fieldName,
});

const isRequired = useCallback(() => {
try {
const tests = fieldValidationSchema
? fieldValidationSchema
.resolve({
value: form?.hasOwnProperty(fieldName) ? form[fieldName] : undefined,
parent: fieldName.includes('.') > 1 && Object.is(form) ? form[fieldName.split('.').slice(0, -1).join('.')] : form,
})
.describe().tests
: false;
return tests ? !!tests.find((test) => test.name === 'required') : false;
} catch (err) {
return false;
}

return false;
}, [form]);
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.