-
Notifications
You must be signed in to change notification settings - Fork 106
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
Configuration File Schema Validation #281
Comments
This would be useful - would have saved me some time yesterday! |
I might take a look at this later. @NuSkooler How do you see it working? A separate tool to begin with? |
@louisnorthmore I haven't put a ton of detailed thought into this, but something like:
I like the idea of veirfy-json since it's light weight and allows custom validators. |
At this point if it hasn't been picked up yet, I change my stance and suggest JSON schema as it's been the clear winner around JSON... schemas. Upon validation error(s), writing to |
Agreed with the overall approach, and this would be fantastic. I know when I was getting started having a broken config was my biggest struggle. The only one I'm not sure about is the last bullet point: "Must allow for additional entries/sections that are not known to the schema" - this would somewhat limit some of the usefulness I think? If someone is trying to debug why a configuration item is not taking effect, etc, this would be easier if there was at least the option to restrict to known elements. |
@cognitivegears I think we can validate things that are in the wrong place, and specifics of all known configuration elements, but consider e.g. a custom mod that is not shipped with enig, but wants some configuration from In JSON schema at least, this probably mostly around allowing additional properties in certain blocks. |
Well, we could validate something that is missing, but not something that is optional but misnamed. And since there are a lot of optional properties in the enigma conf I feel this could be a problem. Not the end of the world for sure, but how about a slightly different approach: Add an optional "mods" property at each level containing an array of objects of "any" type. That way, mods are free to add additional properties, they just have to put it under "mods". So like:
Since it is validated with the "any" type, it doesn't care or validate what is under the mod (if the mod wanted to validate the values itself with it's own schema it could, but doesn't have to.) Still "wild west", just a little more "wild west, but stay in your sandboxes." :) Then we could validate everything else. |
In Here is what I'm thinking (this example with
Another thing we can do, is expose some API(s) that mods can use to easily do their own validation if they want to opt-in.
|
That looks great! Sounds like a good approach to me. |
I suppose another approach for |
Yeah I really like that approach for the main config. This sounds super interesting hope we can do this soon |
I think it makes sense to separate enigma config from mods, etc. It allows us to be more strict about what is in the configuration, and will guide the SysOp to "do the right thing". If they break it, the system will pat them on the back and tell them it's okay and make it better rather than going through frustration or feeling like they can't do it. I can imagine we would want to be able to:
This is an extension of what @NuSkooler was saying early on in this issue. When ./main.js is run and a configuration validation error is found, we can tell the user to run this new oputil function. It will also solidify configuration and allow us to execute "Pre-Flight Checks" in which the system does integrity checks on things like configuration, whether configured ports are in use or not. See Issue #547 which asks for used port detection, as it will currently silently fail to use the specified port if already in use. I think that wants to be a priority. In order to detect if ports are open, we need a very clear and robust way of knowing which ports to check, and whether that port is actually going to be used prior to considering it as important and doing a startup blocking port check. @NuSkooler To move forward, we need a succinct list of all possible configuration values are; can I count on the default config.hjson containing every use case? If yes, I will proceed on writing this as my next opportunity to get familiar. |
@NuSkooler (bump) This will be my next task, please comment on the above. |
I would love this to be worked on for sure. The official JSON Schema makes sense to use for a config schema. The less impactful approach is to probably like in #281 (comment) and we expose some API(s) for custom mods to do self-validation if they want to use a config block within; if they are using their own fully custom configuration files/etc., then they just do whatever they like. |
I suggest that the custom mods have their own configuration outside of the scope of config.hjson. Without me having looked deeply at the configuration logic, do we presently expect / allow mods to extend the config.hjson? In practice, additions to config.hjson would be ignored; however I also think that modification to the original configuration be dissuaded with some sort of warning output during startup such as:
|
Any of the "built" in mods are/can be using the systems config values. I don't believe any custom mods to be adding anything to it, but certainly reach in for config (e.g. I want the board name). Perhaps an API could easily allow enig/HJSON style custom mod config that is in |
Just to clarify that I'm understanding correctly:
|
(1) is correct. (2) is a bit tricky: Rounding up the stuff is probably the hardest bit currently. Searching for |
Then let's do this:
|
That all sounds very reasonable. There are some tools out there that can help a bit in taking an existing document and inferring a schema -- can take care of a good chunk of the busy work.
Compliance via fear and loathing, I love it! |
Using https://www.npmjs.com/package/to-json-schema, I was unable to convert to a schema I'M GUESSING because of how loginServers.ssh.algorithms is interpreted. This is thrown by the to-json-schema library:
|
here is a schema generated by converting my config to json and using a online tool... it may be a OK start. Some things I noticed skimming are some enumerations from entries in things like my file areas, or JSON generated with: |
When I run There is a typo between '--no-color' and '--no-colors' in the cat config. Raised #583 to fix this. |
@NuSkooler I am writing a new function Writing this as a function will allow us to call this function on-demand, making it trivial to update the schema. I ran into a problem, however that you likely know the answer to. Do you know what I could do to solve this? function hardenCurrentConfig() {
// Export Configuration without any flourishes
const config = hjson.rt.parse(fs.readFileSync(getConfigPath(), 'utf8'));
const hjsonOpts = Object.assign({}, HJSONStringifyCommonOpts, {
colors: false,
keepWsc: false,
quotes: "always",
});
const stringConfiguration = hjson.stringify(config, hjsonOpts);
// Load JSON Object
const jsonConfiguration = JSON.parse(stringConfiguration);
// NOTE: This fails, because the stringConfiguration has omitted commas from each line, e.g.
// {
// general: {
// boardName: "My Awesome BBS" <-- missing comma
// prettyBoardName: "|08XXXXX"
// telnetHostname: "xibalba.l33t.codes:44510"
// sshHostname: "xibalba.l33t.codes:44511"
// ...
// Convert to Schema
// const schema = toJsonSchema(stringConfiguration);
// Write to Schema File
// ...
} |
@crhultay I don't think you're going to be able to infer the schema. We don't have the tagging/etc. required to do so in the config. The example I put above could be used as a start, though. Instead of stringifying and re-parsing, just use the object: The HJSON parse() returns a JSON object, you can feed that to whatever you like. |
Here's my line of thinking: As a developer, you will be modifying the config template to add new features / etc ( The user now runs To your point about it missing the tagging, could I ask you for an example of what this should look like? If we put in the effort of tagging in As a side bonus, the hardened schema is user-generated by running config new, so it remains backwards compatible for people running This brings us to the opportunity: Do you think this could be of value, or should I put the crack pipe down? |
@crhultay I guess I'm missing part of what you're suggesting. The A JSON schema would provide a schema to the entire config, and has all the bells and whistles to state and validate. That would easily give us e.g. Sometimes more than a config needs to happen for that, so we need a from-to migrate type system. I think that's a related but separate concern though. |
@NuSkooler Then maybe I'm mistaken, maybe I was thinking of So am I right in thinking that we need to create the schema from the ground up then? I was hoping we could use |
So I've given this some thought, and a bit of research as a guy that's never done JSON Schemas, and here is my new line of thinking: Create a new dir Write a property file, e.g. {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$id": "https://github.com/NuSkooler/enigma-bbs/tree/master/config/schema/loginServers/ssh.schema.json",
"title": "ssh",
"description": "SSH Login Server Configuration",
"type": "object",
"properties": {
"port": {
"description": "TCP Port",
"type": "integer",
"exclusiveMinimum": 0,
},
"enabled": {
"description": "Enable SSH",
"type": "boolean",
},
...
},
"required": [ "port", "enabled", ... ]
} While this is going to be painstaking, it will give us the validation features that we need. @NuSkooler How do you feel about this? |
@crhultay That's basically what I'm thinking. I'm not sure if it should be broken down that much, however. IIRC a lot of tooling doesn't support inclusions, so having the entire config in a single schema file is probably desirable. If you look at the inferred schema I posted, you'll see some similarities to the fragment you posted. IMO it would be a good start (I could take a stab if you like), but will need a lot of touch ups. It's going to be a bit painstaking no matter what, I think. |
@NuSkooler Okay, so day one I will scaffold out a config.schema.json with the elements that I care about. After that, I will write Finally, once we have fleshed out full schema validation we can enhance As an aside, I sent you a reply on Xibalba a few days ago and deleted our old messages from my inbox. When I go to 'e' and then 'i' to check my messages from the main menu, it displays an empty template and then jumps to the previous screen. I feel like this wants to have a new screen that displays 'Send a letter to get a letter' sort of message, otherwise it feels like it's broken to the user. |
It may be nice to have very basic schema validation for configuration files (
config.hjson
,menu.hjson
, etc.).Configuration files are in HJSON, but this directly converts to JSON internally. It may be enough to work with the JSON.
JSON schema validation resources:
See also: #280
The text was updated successfully, but these errors were encountered: