Comments (5)
Never heard about or used this JSON schema feature. Please write a failing spec (test) with the setup requested and the expected result and make a PR. You can also use the existing infra to add your own logic to override or extend the default.
For example, one can allow additional properties, but only if they are each a string:
{
"type": "object",
"properties": {
"number": { "type": "number" },
"street_name": { "type": "string" },
"street_type": { "type": "string",
"enum": ["Street", "Avenue", "Boulevard"]
}
},
"additionalProperties": { "type": "string" }
}
How would this example ideally be reflected using Yup? We could down reverse engineer from there...
from schema-to-yup.
I've added an empty test for additionalProperties
. Please have a go at it ;)
You could extend YupBuilder
class MyYupBuilder extends YupBuilder {
constructor(schema, config = {}) {
super(schema, config);
this.additionalProps = schema.additionalProperties
}
propsToShape(opts) {
const shape = super.propsToShape(opts)
const { additionalProps } = this
if (additionalProps) {
// modify yup shape
}
return shape
}
}
Figure out what yup shape would work in this case, then use the recipe above :) Would love to see a PR for this.
BTW: Just fixed the library core so it should work again. The new ConstraintBuilder
had messed the internals up a bit and was more difficult to get right than anticipated. Sorry about that.
from schema-to-yup.
In the latest commit I've made a slight change to the YupBuilder to make this easier (more flexible infra)
constructor(...) {
this.additionalProps = this.getAdditionalProperties(schema);
}
propsToShape(opts = {}) {
const shape = this.objPropsToShape(opts);
this.objPropsShape = shape;
this.addPropsShape = this.additionalPropsToShape(opts, shape);
return shape;
}
additionalPropsToShape(opts, shape) {
// do your magic here using this.additionalProps
// make new yup constraint function calls on the incoming yup shape object
return shape;
}
from schema-to-yup.
I think you could use the yup test method to add this extra functionality
let jimmySchema = string().test(
'is-jimmy',
'${path} is not Jimmy',
value => value === 'jimmy',
);
The test function would then need a scope with access to the property keys traversed/used as part of the normal validation to check if this property is an additional property not otherwise covered in this context.
const propNamesUsed = Object.keys(this.properties)
const additionalPropertyTestFn = (name, errorFn, (value) => {
if (propNamesUsed.includes(name)) return true; // valid prop if already used
// test logic here
if (value.type == 'string') {
const schema = toYupString(schemaValue)
schema.validateSync(
)
return true
})
shape.test = additionalPropertyTestFn
Perhaps better to use this variant of test
const propNamesUsed = Object.keys(this.properties)
const schemaValue = this.additionalProperties
const additionalPropertyTestFn = (opts) => {
if (propNamesUsed.includes(name)) return true; // valid prop if already used
const args = createSchemaOpts(opts, schemaValue)
// test logic here (ideally generalise to support all supported types)
if (value.type == 'string') {
const schema = toYupString(...args)
return validate(schema, value)
)
return true
})
shape.test = additionalPropertyTestFn
from schema-to-yup.
I've now added a note on this to the Readme of the library
from schema-to-yup.
Related Issues (20)
- Get all properties under a schema HOT 1
- Custom error messages not having any effect HOT 4
- Custom message for type error HOT 18
- which version of yup does schema-to-yup support HOT 3
- "isNullable" is not working for Object type HOT 3
- Update yup to the newest 1.x version HOT 2
- Previously working required message, drops when combining refValueFor
- Previously working required message, drops when combining refValueFor HOT 1
- `ensure` on array does not work/missing example HOT 2
- Tests fail on fresh clone and install of dependencies HOT 3
- There is no regexp function that validate using a pattern which covers Unicode
- `TypeError: Cannot add property required, object is not extensible` on Node server using `esm` HOT 1
- Latest daft version `2020-12` is not supported HOT 2
- Question : why nullable and not type: ["null", ...] ? HOT 7
- Missing support for 'integer' HOT 11
- Multiple when causing Cyclic errors HOT 4
- TypeError: Invalid attempt to spread non-iterable instance.
- Custom 'required' error message doesn't work on fields with 'refValueFor' prop. HOT 1
- Unable to define type objects whichs default may be null
- yup.test
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from schema-to-yup.