diff --git a/docs/docs/04-standard-library/02-std/api-reference.md b/docs/docs/04-standard-library/02-std/api-reference.md index 5501e52a81f..6a3b4cea1fe 100644 --- a/docs/docs/04-standard-library/02-std/api-reference.md +++ b/docs/docs/04-standard-library/02-std/api-reference.md @@ -2090,8 +2090,6 @@ Struct.fromJson(json: Json); Converts a Json to a Struct. -This macro takes a Json object and the Struct definition file name then calls the validate method - ###### `json`Required - *Type:* Json diff --git a/libs/wingc/src/jsify.rs b/libs/wingc/src/jsify.rs index 38feeb8fdf7..4216affb148 100644 --- a/libs/wingc/src/jsify.rs +++ b/libs/wingc/src/jsify.rs @@ -676,7 +676,7 @@ impl<'a> JSifier<'a> { // Any parents we need to get their properties for e in extends { code.line(format!( - "...require(\"{}\")().getSchema().properties,", + "...require(\"{}\")().jsonSchema().properties,", struct_filename(&e.root.name) )) } @@ -710,7 +710,7 @@ impl<'a> JSifier<'a> { let mut required: Vec = vec![]; // fields that are required let mut dependencies: Vec = vec![]; // schemas that need added to validator - code.open("static getSchema() {".to_string()); + code.open("static jsonSchema() {".to_string()); code.open("return {"); code.line(format!("id: \"/{}\",", name)); code.line("type: \"object\",".to_string()); @@ -740,7 +740,7 @@ impl<'a> JSifier<'a> { // pull in all required fields from parent structs for e in extends { code.line(format!( - "...require(\"{}\")().getSchema().required,", + "...require(\"{}\")().jsonSchema().required,", struct_filename(&e.root.name) )); } @@ -751,14 +751,14 @@ impl<'a> JSifier<'a> { code.open("$defs: {"); for dep in &dependencies { code.line(format!( - "\"{}\": {{ type: \"object\", \"properties\": require(\"{}\")().getSchema().properties }},", + "\"{}\": {{ type: \"object\", \"properties\": require(\"{}\")().jsonSchema().properties }},", dep, struct_filename(&dep) )); } for e in extends { code.line(format!( - "...require(\"{}\")().getSchema().$defs,", + "...require(\"{}\")().jsonSchema().$defs,", struct_filename(&e.root.name) )); } @@ -769,7 +769,7 @@ impl<'a> JSifier<'a> { // create _validate() function code.open("static fromJson(obj) {"); - code.line("return stdStruct._validate(obj, this.getSchema())"); + code.line("return stdStruct._validate(obj, this.jsonSchema())"); code.close("}"); // create _toInflightType function that just requires the generated struct file diff --git a/libs/wingc/src/type_check.rs b/libs/wingc/src/type_check.rs index c971083ff05..b4034f94838 100644 --- a/libs/wingc/src/type_check.rs +++ b/libs/wingc/src/type_check.rs @@ -4290,7 +4290,10 @@ impl<'a> TypeChecker<'a> { } } Type::Struct(ref s) => { - if property.name == "fromJson" || property.name == "tryFromJson" { + let from_json = "fromJson"; + let try_from_json = "tryFromJson"; + + if property.name == from_json || property.name == try_from_json { // we need to validate that only structs with all valid json fields can have a fromJson method for (name, field) in s.fields(true) { if !field.has_json_representation() { diff --git a/libs/wingsdk/src/std/struct.ts b/libs/wingsdk/src/std/struct.ts index 3d02361ac38..4398c77b62b 100644 --- a/libs/wingsdk/src/std/struct.ts +++ b/libs/wingsdk/src/std/struct.ts @@ -19,8 +19,6 @@ export class Struct { /** * Converts a Json to a Struct * - * This macro takes a Json object and the Struct definition file name then calls the validate method - * * @macro ($self$.fromJson($args$)) */ public static fromJson(json: Json): T1 { diff --git a/tools/hangar/__snapshots__/test_corpus/sdk_tests/bucket/events.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/sdk_tests/bucket/events.w_compile_tf-aws.md index 38ff094bc70..4ffe326a213 100644 --- a/tools/hangar/__snapshots__/test_corpus/sdk_tests/bucket/events.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/sdk_tests/bucket/events.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class CheckHitCountOptions { - static getSchema() { + static jsonSchema() { return { id: "/CheckHitCountOptions", type: "object", @@ -21,12 +21,12 @@ module.exports = function(stdStruct, fromInline) { "count", ], $defs: { - "Source": { type: "object", "properties": require("./Source.Struct.js")().getSchema().properties }, + "Source": { type: "object", "properties": require("./Source.Struct.js")().jsonSchema().properties }, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./CheckHitCountOptions.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/bring_local.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/bring_local.w_compile_tf-aws.md index 451442da165..0599af1b4d0 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/bring_local.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/bring_local.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class Point { - static getSchema() { + static jsonSchema() { return { id: "/Point", type: "object", @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Point.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/deep_equality.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/deep_equality.w_compile_tf-aws.md index 4c44a60b08b..f63bd87ec8e 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/deep_equality.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/deep_equality.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class Cat { - static getSchema() { + static jsonSchema() { return { id: "/Cat", type: "object", @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Cat.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/inflight_class_as_struct_members.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/inflight_class_as_struct_members.w_compile_tf-aws.md index 603b3e2b33f..5f872e30c2b 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/inflight_class_as_struct_members.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/inflight_class_as_struct_members.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class Bar { - static getSchema() { + static jsonSchema() { return { id: "/Bar", type: "object", @@ -15,12 +15,12 @@ module.exports = function(stdStruct, fromInline) { "foo", ], $defs: { - "Foo": { type: "object", "properties": require("./Foo.Struct.js")().getSchema().properties }, + "Foo": { type: "object", "properties": require("./Foo.Struct.js")().jsonSchema().properties }, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Bar.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md index a7526fc1603..ad04fccacd1 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/optionals.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class Name { - static getSchema() { + static jsonSchema() { return { id: "/Name", type: "object", @@ -20,7 +20,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Name.Struct.js")(${ context._lift(stdStruct) })`); @@ -35,7 +35,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Payload { - static getSchema() { + static jsonSchema() { return { id: "/Payload", type: "object", @@ -48,12 +48,12 @@ module.exports = function(stdStruct, fromInline) { "a", ], $defs: { - "cloud": { type: "object", "properties": require("./cloud.Struct.js")().getSchema().properties }, + "cloud": { type: "object", "properties": require("./cloud.Struct.js")().jsonSchema().properties }, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Payload.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/store.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/store.w_compile_tf-aws.md index acdb0df6c15..7e637f6ef1b 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/store.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/store.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class Point { - static getSchema() { + static jsonSchema() { return { id: "/Point", type: "object", @@ -21,7 +21,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Point.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.w_compile_tf-aws.md index 7e9419b7a10..1b4f392312e 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/struct_from_json.w_compile_tf-aws.md @@ -4,25 +4,25 @@ ```js module.exports = function(stdStruct, fromInline) { class Advisor { - static getSchema() { + static jsonSchema() { return { id: "/Advisor", type: "object", properties: { - ...require("./Person.Struct.js")().getSchema().properties, + ...require("./Person.Struct.js")().jsonSchema().properties, employeeID: { type: "string" }, }, required: [ "employeeID", - ...require("./Person.Struct.js")().getSchema().required, + ...require("./Person.Struct.js")().jsonSchema().required, ], $defs: { - ...require("./Person.Struct.js")().getSchema().$defs, + ...require("./Person.Struct.js")().jsonSchema().$defs, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Advisor.Struct.js")(${ context._lift(stdStruct) })`); @@ -37,25 +37,25 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Bar { - static getSchema() { + static jsonSchema() { return { id: "/Bar", type: "object", properties: { - ...require("./Foo.Struct.js")().getSchema().properties, + ...require("./Foo.Struct.js")().jsonSchema().properties, b: { type: "number" }, }, required: [ "b", - ...require("./Foo.Struct.js")().getSchema().required, + ...require("./Foo.Struct.js")().jsonSchema().required, ], $defs: { - ...require("./Foo.Struct.js")().getSchema().$defs, + ...require("./Foo.Struct.js")().jsonSchema().$defs, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Bar.Struct.js")(${ context._lift(stdStruct) })`); @@ -70,7 +70,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Course { - static getSchema() { + static jsonSchema() { return { id: "/Course", type: "object", @@ -87,7 +87,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Course.Struct.js")(${ context._lift(stdStruct) })`); @@ -102,7 +102,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class CourseResults { - static getSchema() { + static jsonSchema() { return { id: "/CourseResults", type: "object", @@ -117,13 +117,13 @@ module.exports = function(stdStruct, fromInline) { "dateTaken", ], $defs: { - "Course": { type: "object", "properties": require("./Course.Struct.js")().getSchema().properties }, - "Date": { type: "object", "properties": require("./Date.Struct.js")().getSchema().properties }, + "Course": { type: "object", "properties": require("./Course.Struct.js")().jsonSchema().properties }, + "Date": { type: "object", "properties": require("./Date.Struct.js")().jsonSchema().properties }, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./CourseResults.Struct.js")(${ context._lift(stdStruct) })`); @@ -138,7 +138,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Date { - static getSchema() { + static jsonSchema() { return { id: "/Date", type: "object", @@ -157,7 +157,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Date.Struct.js")(${ context._lift(stdStruct) })`); @@ -172,7 +172,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Foo { - static getSchema() { + static jsonSchema() { return { id: "/Foo", type: "object", @@ -187,7 +187,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Foo.Struct.js")(${ context._lift(stdStruct) })`); @@ -202,7 +202,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Foosible { - static getSchema() { + static jsonSchema() { return { id: "/Foosible", type: "object", @@ -216,7 +216,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Foosible.Struct.js")(${ context._lift(stdStruct) })`); @@ -231,7 +231,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Person { - static getSchema() { + static jsonSchema() { return { id: "/Person", type: "object", @@ -246,12 +246,12 @@ module.exports = function(stdStruct, fromInline) { "dob", ], $defs: { - "Date": { type: "object", "properties": require("./Date.Struct.js")().getSchema().properties }, + "Date": { type: "object", "properties": require("./Date.Struct.js")().jsonSchema().properties }, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Person.Struct.js")(${ context._lift(stdStruct) })`); @@ -266,12 +266,12 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Student { - static getSchema() { + static jsonSchema() { return { id: "/Student", type: "object", properties: { - ...require("./Person.Struct.js")().getSchema().properties, + ...require("./Person.Struct.js")().jsonSchema().properties, enrolled: { type: "boolean" }, schoolId: { type: "string" }, advisor: { "$ref": "#/$defs/Advisor" }, @@ -282,18 +282,18 @@ module.exports = function(stdStruct, fromInline) { required: [ "enrolled", "schoolId", - ...require("./Person.Struct.js")().getSchema().required, + ...require("./Person.Struct.js")().jsonSchema().required, ], $defs: { - "Advisor": { type: "object", "properties": require("./Advisor.Struct.js")().getSchema().properties }, - "Course": { type: "object", "properties": require("./Course.Struct.js")().getSchema().properties }, - "CourseResults": { type: "object", "properties": require("./CourseResults.Struct.js")().getSchema().properties }, - ...require("./Person.Struct.js")().getSchema().$defs, + "Advisor": { type: "object", "properties": require("./Advisor.Struct.js")().jsonSchema().properties }, + "Course": { type: "object", "properties": require("./Course.Struct.js")().jsonSchema().properties }, + "CourseResults": { type: "object", "properties": require("./CourseResults.Struct.js")().jsonSchema().properties }, + ...require("./Person.Struct.js")().jsonSchema().$defs, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Student.Struct.js")(${ context._lift(stdStruct) })`); diff --git a/tools/hangar/__snapshots__/test_corpus/valid/structs.w_compile_tf-aws.md b/tools/hangar/__snapshots__/test_corpus/valid/structs.w_compile_tf-aws.md index fa4d3180a65..e61ce8a61a3 100644 --- a/tools/hangar/__snapshots__/test_corpus/valid/structs.w_compile_tf-aws.md +++ b/tools/hangar/__snapshots__/test_corpus/valid/structs.w_compile_tf-aws.md @@ -4,7 +4,7 @@ ```js module.exports = function(stdStruct, fromInline) { class A { - static getSchema() { + static jsonSchema() { return { id: "/A", type: "object", @@ -19,7 +19,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./A.Struct.js")(${ context._lift(stdStruct) })`); @@ -34,12 +34,12 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class B { - static getSchema() { + static jsonSchema() { return { id: "/B", type: "object", properties: { - ...require("./A.Struct.js")().getSchema().properties, + ...require("./A.Struct.js")().jsonSchema().properties, field1: { type: "number" }, field2: { type: "string" }, field3: { "$ref": "#/$defs/A" }, @@ -48,16 +48,16 @@ module.exports = function(stdStruct, fromInline) { "field1", "field2", "field3", - ...require("./A.Struct.js")().getSchema().required, + ...require("./A.Struct.js")().jsonSchema().required, ], $defs: { - "A": { type: "object", "properties": require("./A.Struct.js")().getSchema().properties }, - ...require("./A.Struct.js")().getSchema().$defs, + "A": { type: "object", "properties": require("./A.Struct.js")().jsonSchema().properties }, + ...require("./A.Struct.js")().jsonSchema().$defs, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./B.Struct.js")(${ context._lift(stdStruct) })`); @@ -72,7 +72,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Dazzle { - static getSchema() { + static jsonSchema() { return { id: "/Dazzle", type: "object", @@ -87,7 +87,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Dazzle.Struct.js")(${ context._lift(stdStruct) })`); @@ -102,7 +102,7 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Razzle { - static getSchema() { + static jsonSchema() { return { id: "/Razzle", type: "object", @@ -117,7 +117,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Razzle.Struct.js")(${ context._lift(stdStruct) })`); @@ -132,26 +132,26 @@ module.exports = function(stdStruct, fromInline) { ```js module.exports = function(stdStruct, fromInline) { class Showtime { - static getSchema() { + static jsonSchema() { return { id: "/Showtime", type: "object", properties: { - ...require("./Razzle.Struct.js")().getSchema().properties, - ...require("./Dazzle.Struct.js")().getSchema().properties, + ...require("./Razzle.Struct.js")().jsonSchema().properties, + ...require("./Dazzle.Struct.js")().jsonSchema().properties, }, required: [ - ...require("./Razzle.Struct.js")().getSchema().required, - ...require("./Dazzle.Struct.js")().getSchema().required, + ...require("./Razzle.Struct.js")().jsonSchema().required, + ...require("./Dazzle.Struct.js")().jsonSchema().required, ], $defs: { - ...require("./Razzle.Struct.js")().getSchema().$defs, - ...require("./Dazzle.Struct.js")().getSchema().$defs, + ...require("./Razzle.Struct.js")().jsonSchema().$defs, + ...require("./Dazzle.Struct.js")().jsonSchema().$defs, } } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./Showtime.Struct.js")(${ context._lift(stdStruct) })`); @@ -201,7 +201,7 @@ module.exports = function({ }) { ```js module.exports = function(stdStruct, fromInline) { class lotsOfTypes { - static getSchema() { + static jsonSchema() { return { id: "/lotsOfTypes", type: "object", @@ -229,7 +229,7 @@ module.exports = function(stdStruct, fromInline) { } } static fromJson(obj) { - return stdStruct._validate(obj, this.getSchema()) + return stdStruct._validate(obj, this.jsonSchema()) } static _toInflightType(context) { return fromInline(`require("./lotsOfTypes.Struct.js")(${ context._lift(stdStruct) })`);