threefoldtech / tfgrid-sdk-ts

Apache License 2.0
4 stars 8 forks source link

🐞 [Bug]: Add validation to compute capacity module #2821

Closed zaelgohary closed 1 month ago

zaelgohary commented 4 months ago

Is there an existing issue for this?

which package/s did you face the problem with?

grid_client

What happened?

No errors were thrown when testing with invalid min, max, empty values, and decimal values.

Steps To Reproduce

Run the following test: https://github.com/threefoldtech/tfgrid-sdk-ts/pull/2820/files

which network/s did you face the problem on?

Dev

version

Locally

Twin ID/s

No response

Node ID/s

No response

Farm ID/s

No response

Contract ID/s

No response

Relevant screenshots/screen records

-

Relevant log output

-
MohamedElmdary commented 3 months ago

We can add a MethodDecorator which should validate object before trigger the targeted function

function TriggerValidate(): MethodDecorator {
  return <T>(
    target: object,
    propertyKey: string | symbol,
    descriptor: TypedPropertyDescriptor<T>,
  ): void | TypedPropertyDescriptor<T> => {
    function value(...args: any[]): any {
      const errors = validateSync(target);

      if (errors.length > 0) {
        console.log({ errors });
        throw new Error("Something went wrong");
      }

      return target[propertyKey]?.(...args);
    }

    return {
      ...descriptor,
      value: value as T,
    };
  };
}

which can be used like

class Zmount extends WorkloadData {
  @TriggerValidate()
  challenge(): string {
    return ''
  }
}

image

MohamedElmdary commented 3 months ago

We can update how field work using get/set example below:

https://www.typescriptlang.org/play/?experimentalDecorators=true&emitDecoratorMetadata=true#code/GYVwdgxgLglg9mABAFQE4wOYYKaoGoCGANjACYFTYAUAlAFyICy2UAFnKQCLYRyoV9EAbwBQiRKhYhUSUJFgJEAHmQA+KmPGIoBVDigM4AIwBWPKABpN4gA6o4N3FACeAaWzOGAZyjowGRAAfRC9nAFsjOCIrLURSbC8IdBsoPgZkZ0dSAAV7R1QXbkTk1NQVVRjEekQANzgyIJRM7By8p2cipJgUvnLha0QiFkQAfRriEGwGAjBnAG4ByShpJFFY8QA6Lfji7tLK2P1afvWtXjAvKOwNojgMKgByfUpURAB3PgBrGH8Q9hAiKQJFIZCFSthahNsA8aANYksVqNxkRJnDEABfA5aLwsKhgbBvQgo7A0E6nRDnS5DG53R44qAvd5fH4BLz-QGQkjkSgwtHiMZQxAAXkQ+MJULR6MQA3RC3EspE6JEIggRAIXi8iAAqjjXmstAABNCYHD4YhkCjUUlgAhhbAAQm8vhZCyVKoQPkQIF1wtFBO1uto7ouVxp929uA2NrtsIjqCjtohIoejDgrETpAewap11u4d1CZjyqAA

zaelgohary commented 3 months ago

Work completed:

Implemented a way to validate class prop/methods when set/call.

khaledyoussef24 commented 2 months ago

verified compute capacity is now added to the gridclient tests module and ran the test and it is working fine

Image Image Image