Skip to content
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

Json: support unsafe schema validation #3788

Closed
hasanaburayyan opened this issue Aug 11, 2023 · 2 comments · Fixed by #4334
Closed

Json: support unsafe schema validation #3788

hasanaburayyan opened this issue Aug 11, 2023 · 2 comments · Fixed by #4334
Assignees
Labels
🛠️ compiler Compiler good first issue Good for newcomers 📜 lang-spec-impl Appears in the language spec roadmap
Milestone

Comments

@hasanaburayyan
Copy link
Contributor

The following sections need to be added to language reference:

1.1.4.5 Schema validation (append)

Use unsafe: true to disable this check at your own risk (P2):

let trustMe = Json [1,2,3];
let x = Array<num>.fromJson(trustMe, unsafe: true);
assert(x.at(1) == 2);
1.1.4.7 Assignment to user-defined structs (append)

Same as with primitives and containers, it is possible to opt-out of validation using unsafe: true:

let p = Json { first: "Wing", phone: 1234 };
let x = Contact.fromJson(p, unsafe: true);
assert(x.last.len > 0);
// RUNTIME ERROR: Cannot read properties of undefined (reading 'length')
@hasanaburayyan hasanaburayyan added 📜 lang-spec-impl Appears in the language spec roadmap 🛠️ compiler Compiler labels Aug 11, 2023
mergify bot pushed a commit that referenced this issue Aug 12, 2023
The changes in this PR make it possible to call `fromJson` on any compatible struct definition. 

## Implementation notes:
Previously we treated the jsification of structs as a no-op since there is not a JS equivalent. So with this change structs now JSify into a `Struct` file that contains a class with a static `jsonSchema` and a `fromJson` function which will allow for field validation at runtime. 

The schema generated adheres to: https://json-schema.org/understanding-json-schema/

take this simple example Wing code:
```js
struct MyStruct {
	myField: str;
	myOtherField: num;
}
```

this will now generate a JS file named `MyStruct.Struct.js` which looks like this:
```js
module.exports = function(stdStruct, fromInline) {
  class MyStruct {
    static jsonSchema() {
      return {
        id: "/MyStruct",
        type: "object",
        properties: {
          myField: { type: "string" },
          myOtherField: { type: "number" },
        },
        required: [
          "myField",
          "myOtherField",
        ],
        $defs: {
        }
      }
    }
    static fromJson(obj) {
      return stdStruct._validate(obj, this.jsonSchema())
    }
    static _toInflightType(context) {
      return fromInline(`require("./MyStruct.Struct.js")(${ context._lift(stdStruct) })`);
    }
  }
  return MyStruct;
};

```

The piece that brings this all together is the addition of the `Struct` class in our std that only has a `fromJson()` methods at the moment that is a Wing macro. The macro just calls the `fromJson()` method in the generated javascript.

### Misc
We want to stop the user at compile time from calling `fromJson` on a struct that cannot be represented by a Json value ie
```js
struct MyStruct {
	b: cloud.Bucket;
}
let j = {};
MyStruct.fromJson(j);
```

to prevent this I added a check in the typechecker for structs to confirm that if `fromJson` is called that all the fields in the struct are valid for conversion attempt

See image below for error when attempting:
<img width="664" alt="image" src="https://github.com/winglang/wing/assets/45375125/785a2fa6-8823-4fa2-aaa5-4bc8f7ed597f">


Closes: #3653
Closes: #3139

## TODO:
- [x] separate the work done here and the remaining work into different tickets.
- [x] update language reference

## Followup issues that are out of scope for this PR:
- #3792
- #3790
- #3789
- #3788

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
@staycoolcall911
Copy link
Contributor

@hasanaburayyan thanks for opening these issues to track on our language roadmap. On top of marking them with the lang-spec-impl label (which you added) I also added them on the Wing Language Roadmap project.

@staycoolcall911 staycoolcall911 added the good first issue Good for newcomers label Aug 17, 2023
@hasanaburayyan hasanaburayyan self-assigned this Sep 2, 2023
@staycoolcall911 staycoolcall911 added this to the Containers milestone Sep 12, 2023
@mergify mergify bot closed this as completed in #4334 Sep 29, 2023
mergify bot pushed a commit that referenced this issue Sep 29, 2023
Add support for using `unsafe: true` when calling `fromJson()` methods

Closes: #3788

## Checklist

- [x] Title matches [Winglang's style guide](https://www.winglang.io/contributing/start-here/pull_requests#how-are-pull-request-titles-formatted)
- [x] Description explains motivation and solution
- [x] Tests added (always)
- [x] Docs updated (only required for features)
- [x] Added `pr/e2e-full` label if this feature requires end-to-end testing

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
@monadabot
Copy link
Contributor

Congrats! 🚀 This was released in Wing 0.34.2.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🛠️ compiler Compiler good first issue Good for newcomers 📜 lang-spec-impl Appears in the language spec roadmap
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants