-
Notifications
You must be signed in to change notification settings - Fork 114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Redesign schema package to allow any FieldValidator at the top level #77
Comments
Primarily use case. I wanted to use a schema in array types. Previously you could only use a type which didn't support schema. |
Ah, ok so "meta": Field{Validator: schema.String{}, Schema: subSchema} Would it be sensible to deprecate/remove the Schema attribute from Field - perhaps after tagging a release, as suggested in #65? I assume this could also simplify things internally and avoid handling the same case twice several places? For one I realize we don't handle Field.Schema in the |
Yes I think should remove For #65 I would rather wait for the Go official way to handle that. |
OK, for the manifest part, but tagging a release in Git (e.g. v0.1.0), is something the team have already recommended us to do, and for end-users, rest-layer would be a lot safer/easier to use if you could rely on a tagged release. Especially when we do breaking changes. |
Sure we can tag. |
@rs - updated the title & description. |
Are you going to submit a PR for this one? |
I don't know if I can/want to figure out the implications on reference checking... there are some other easier issues I would rather look at, e.g. JSON schema ones... |
At least not know |
@rs Looking at the definition of Object:
Could we not make Schema implement FieldValidator or at least change the definition to:
|
Maybe it's not so easy btw. At least not without introducing additional breaking changes everywhere... It would be nice to make the API consistent by removing the Schema attribute, while still avoiding the additional level of nesting/indenting you get by using schema.Object. |
I'm all for introducing breaking changes now that simplify the API rather than later. |
I have done some background thinking on this, and I have a rather radical proposal:
Part of the reasoning for this, is that I have a use-case to support something like this at a top level: s := schema.Schema{
Description: "My resource",
Validator: &schema.AllOf{
&schema.Object{
Fields: commonFields,
},
$schema.AnyOf{
$schema.Object{Fields: schema.Fields{
"type":{Validator:&schema.String{Allowed:[]string{"x"}}}},
"someField":{Validator: validatorX},
},
$schema.Object{Fields: schema.Fields{
"type":{Validator:&schema.String{Allowed:[]string{"y"}}}},
"someField":{Validator: validatorY},
}
}
}
} I can do this today via a |
The error type returned from |
Interesting idea. Then instead of The root as Object might be enforced in |
IN #151, I am proposing to replace However, for all except Validator: &schema.Dict{
Required: []string{"fieldA", "fieldB"},
KeyValidator: &schema.String{},
Values: schema.Field{...}
} And also translate that to JSON Schema:
But more importantly, we would not have a unused |
I want to drag this a little bit further. I think the type Object struct{
Fields map[string]Schema
Required []string
}
type Object struct{
Fields map[string]Schema
Required []string
}
type Schema struct{
Title string
Description string
ReadOnly bool
Type Validator
} Or, a variant taking #194 into consideration: type SchemaBuilder struct{
Title string
Description string
ReadOnly bool
Type ValidatorBuilder
}
// A compiled schema of some sort...
type Schema interface{
Title() string
Description() string
ReadOnly() bool
Validate(ctx context.Context, changes, original interface{}) error
JSONSchema() (map[string]interface{}, error)
} For now it's just an idea to remember when starting on the actual implementation (some time in the future). |
I plan to start this issue by:
I will probably take my time, and try to address as many design issues as possible, and then we can see what we want to do when it's done. |
Why not doing this in a branch? |
We use rest-layer actively. We are pretty satisfied with it, but over time we have hit most of the limitations regarding the current schema package at some point, and either worked around it, or left it in a slightly undesirable state. To get up with a design that addresses all these issues, I basically first want to proto-type it, without having to take the other rest-layer packages (or even most of the schema package) into account. In this proto-type I want to attempt to solve this issue as well as some linked issues (e.g. #194 and #192, and maybe #138, and perhaps other issues that I can think of. This may need to happen in several iterations. The proto-type will not copy the query package. Once the proto-type is done, I see two possible futures for the schema package:
For case 2, the query package would necessarily remain in rest-layer, but except for that, there is not much dependencies on the schema validation/serialization package itself that can not be solved by a simple interface (making the schema-package pluggable). PS! This isn't really a goal though, so most likely it would still be 1. For the proto-type, weather we choose 1 or 2 is mostly irrelevant. |
Link to proto-type for new schema design: It's not in a working state. |
UPDATED on 2018-09-05. Originally this was a question between the difference of the schema.Object FieldValidator and the Schema paramter on Field, and it evolved from there.
Background
Today there are two ways to specify a schema:
and
These are equivalent in principal, but in reality, the code supports them differently.
E.g. for validating and correctly setting e.g. read-only values, only the first method will work.
The second way of expressing this, the only one supported by the JSON-schmea encoding package, is the only syntax that allowed Schema validation within an Array, AnyOff, AllOf or other nested structure in the past.
Proposal
This ticket suggests a redesign of the schema package so that:
FieldValidator
to be used in a top-level schema (renamed to just schema.Validator in example).schema.Schema
andschema.Field
types into oneRequired
Field attribute (does not make sense on all fields) to be a list-attribute on Object.Example struct definitions:
The text was updated successfully, but these errors were encountered: