2017 © Pedro Peláez
 

library type-assert

Convert untyped data to typed data

image

hhvm/type-assert

Convert untyped data to typed data

  • Wednesday, May 9, 2018
  • by fredemmott
  • Repository
  • 5 Watchers
  • 11 Stars
  • 14,510 Installations
  • Hack
  • 15 Dependents
  • 0 Suggesters
  • 4 Forks
  • 5 Open issues
  • 17 Versions
  • 60 % Grown

The README.md

TypeAssert

Continuous Integration, (*1)

Hack library for converting untyped data to typed data., (*2)

Warning for TypeAssert\matches_type_structure()

TypeStructure<T>, type_structure(), and ReflectionTypeAlias::getTypeStructures() are experimental features of HHVM, and not supported by Facebook or the HHVM team. This means that matches_type_structure() may need to be removed in a future release without warning., (*3)

We strongly recommend moving to TypeAssert\matches<T>() and TypeCoerce\match<T>() instead., (*4)

Installation

composer require hhvm/type-assert

Usage

TypeAssert provides functions that take a mixed input, and will either return it unmodified (but with type data) or throw an exception; for example:, (*5)

<?hh // strict
use namespace Facebook\TypeAssert;
function needs_string(string $bar): void {
}

function main(): void {
  needs_string(TypeAssert\string('foo')); // type-safe and works fine
  needs_string(TypeAssert\string(123)); // type-safe, but throws
}

These include: - string(mixed): string - int(mixed): int - float(mixed): float - bool(mixed): bool - resource(mixed): resource - num(mixed): num - arraykey(mixed): arraykey - not_null<T>(?T): T - instance_of<T>(classname<T>, mixed): T - classname_of<T>(classname<T>, mixed): classname<T> - matches<T>(mixed): T - matches_type_structure<T>(TypeStructure<T>, mixed): T, (*6)

Coercion

TypeAssert also contains the Facebook\TypeCoerce namespace, which includes a similar set of functions:, (*7)

  • string(mixed): string
  • int(mixed): int
  • float(mixed): float
  • bool(mixed): bool
  • resource(mixed): resource
  • num(mixed): num
  • arraykey(mixed): arraykey
  • match<T>(mixed): T
  • match_type_structure<T>(TypeStructure<T>, mixed): T

These will do 'safe' transformations, such as int-ish strings to int, ints to strings, arrays to vecs, arrays to dicts, and so on., (*8)

TypeSpec

You can also assert/coerce complex types (except for shapes and tuples) without a type_structure:, (*9)

<?hh

use namespace Facebook\TypeSpec;

$spec = TypeSpec\dict(
  TypeSpec\string(),
  TypeSpec\int(),
);
$x = $spec->assertType(dict['foo' => 123]); // passes: $x is a dict<string, int>
$x = $spec->assertType(dict['foo' => '123']); // fails
$x = $spec->assertType(dict[123 => 456]); // fails
$x = $spec->assertType(dict[123 => 456]); // fails

$x = $spec->coerceType(dict[123 => '456']); // passes: $x is dict['123' => 456];

Shapes and tuples are not supported, as they can not be expressed generically., (*10)

matches_type_structure<T>(TypeStructure<T>, mixed): T

Asserts that a variable matches the given type structure; these can be arbitrary nested shapes. This is particular useful for dealing with JSON responses., (*11)

<?hh // strict

use namespace Facebook\TypeAssert;

class Foo {
  const type TAPIResponse = shape(
    'id' => int,
    'user' => string,
    'data' => shape(
      /* ... */
    ),
  );

  public static function getAPIResponse(): self::TAPIResponse {
    $json_string = file_get_contents('https://api.example.com');
    $array = json_decode($json_string, /* associative = */ true);
    return TypeAssert\matches_type_structure(
      type_structure(self::class, 'TAPIResponse'),
      $array,
    );
  }
}

You can use type_structure() to get a TypeStructure<T> for a type constant, or ReflectionTypeAlias::getTypeStructure() for top-level type aliases., (*12)

not_null<T>(?T): T

Throws if it's null, and refines the type otherwise - for example:, (*13)

<?hh // strict
use namespace \Facebook\TypeAssert;

function needs_string(string $foo): void {}
function needs_int(int $bar): void {}

function main(?string $foo, ?int bar): void {
  needs_string(TypeAssert\not_null($foo)); // ?string => string
  needs_int(TypeAssert\not_null($bar)); // ?int => int
}

is_instance_of<T>(classname<T>, mixed): T

Asserts that the input is an object of the given type; for example:, (*14)

<?hh
use namespace Facebook\TypeAssert;

class Foo {}

function needs_foo(Foo $foo): void {}

function main(mixed $foo): void {
  needs_foo(TypeAssert::is_instance_of(Foo::class, $foo));
}

main(new Foo());

is_classname_of<T>(classname<T>, mixed): classname<T>

Asserts that the input is the name of a child of the specified class, or implements the specified interface., (*15)

<?hh // strict
use namespace Facebook\TypeAssert;

class Foo {
  public static function doStuff(): void {}
}
class Bar extends Foo {
  <<__Override>>
  public static function doStuff(): void {
    // specialize here
  }
}

function needs_foo_class(classname<Foo> $foo): void {
  $foo::doStuff();
}

function main(mixed $class): void {
  needs_foo_class(TypeAssert::is_classname_of(Foo::class, $class));
}

main(Bar::class);

Credit

This library is a reimplementation of ideas from:, (*16)

  • @admdikramr
  • @ahupp
  • @dlreeves
  • @periodic1236
  • @schrockn

Security Issues

We use GitHub issues to track public bugs. Please ensure your description is clear and has sufficient instructions to be able to reproduce the issue., (*17)

Facebook has a bounty program for the safe disclosure of security bugs. In those cases, please go through the process outlined on that page and do not file a public issue., (*18)

License

Type-Assert is MIT-licensed., (*19)

The Versions

09/05 2018

dev-master

9999999-dev

Convert untyped data to typed data

  Sources   Download

MIT

The Requires

 

The Development Requires

hack typeassert

09/05 2018

v3.2.2

3.2.2.0

Convert untyped data to typed data

  Sources   Download

MIT

The Requires

 

The Development Requires

hack typeassert

09/02 2018

v3.2.1

3.2.1.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

08/01 2018

v3.2.0

3.2.0.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

09/11 2017

v3.1.0

3.1.0.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

26/10 2017

v3.0.x-dev

3.0.9999999.9999999-dev

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

26/10 2017

v3.0.4

3.0.4.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

02/10 2017

v3.0.3

3.0.3.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

28/09 2017

v3.0.2

3.0.2.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

20/09 2017

v3.0.1

3.0.1.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

14/09 2017

v3.0

3.0.0.0

Convert untyped data to typed data

  Sources   Download

The Requires

 

The Development Requires

hack typeassert

29/08 2017

v2.0

2.0.0.0

Convert untyped data to typed data

  Sources   Download

The Requires

  • hhvm ~3.12

 

The Development Requires

hack typeassert

15/08 2017

v1.1.1

1.1.1.0

Convert untyped data to typed data

  Sources   Download

ISC

The Requires

  • hhvm ~3.12

 

The Development Requires

hack typeassert

15/02 2017

v1.1

1.1.0.0

Convert untyped data to typed data

  Sources   Download

ISC

The Requires

  • hhvm ~3.12

 

The Development Requires

hack typeassert

04/12 2016

v1.0

1.0.0.0

Convert untyped data to typed data

  Sources   Download

ISC

The Requires

  • hhvm ~3.12

 

The Development Requires

hack typeassert

28/09 2016

v0.2

0.2.0.0

Convert untyped data to typed data

  Sources   Download

ISC

The Requires

  • hhvm ~3.12

 

The Development Requires

hack typeassert

28/09 2016

v0.1

0.1.0.0

Convert untyped data to typed data

  Sources   Download

ISC

The Requires

  • hhvm ~3.9

 

The Development Requires

hack typeassert