Download the PHP package le0daniel/typescript-schema without Composer

On this page you can find all versions of the php package le0daniel/typescript-schema. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package typescript-schema

Typescript Schema

This is a simple library with no outside dependencies that generates a typescript schema out of your defined schema. This can help creating tight contracts in rest APIs for your input and output, without the need and complexity of using GraphQL or similar.

Installation

Getting started

Define your schema for input or output.

Difference between parse and serialize

The main difference is that parsing will cast some values to PHP types, whereas serialize will return a format that is JSON serializable. Use parse if you have unknown input that should be used in a PHP, use serialize if the result should be Json Serialized.

Parsing: Takes input and returns PHP Types where possible (Enums, DateTimeImmutable) Serializing: Takes input and serializes it to JsonSerializable format. Example (Enum => Enum->name, DateTimeImmutable => DateTimeImmutable->format(...))

Value Object Casting

It is possible to cast values as value objects. This is done using reflection. In case the object has a constructor, the constructor is used. Otherwise, a new object is created and the properties are set. Important, the latter case does not work with readonly properties. You can customize the casting logic by having a public static function cast() set on your class. In this case, this method is called with the raw data. No other logic applies.

Typescript Support

By default, the toDefinition() method generates Json Schema output. You can use it to create a valid typescript declaration using the Typescript utility.

Primitive types

Coercion of values

By default, type checks are strict. Meaning passing a number to a String type will result in a failure. You can use coercion to make it less strict as following: Schema::string()->coerce(). This is available for all primitive types and tries to parse the value from any input.

This works as following for the different primitives:

String

The string type supports following default validations:

Int

The int type supports following validations:

DateTime

The DateTime primitive accepts by default a string (in given format, defaults to DateTime::ATOM) or an instance of the DateTimeInterface. All instances are cast into a DateTimeImmutable.

By default, the output type is as following: {date: string, timezone_type: number, timezone: string}. This is the default JsonSerialization of a DateTime class in PHP.

For output, it is useful to use Schema::dateTime()->asFormattedString(), which will return the formatted string instead of the DateTimeImmutableInstance.

Enum

An Enum type expects the input to be the Enum instance or a string with the name of the enum. In serialization mode, the Enum name is outputted (Enum::CASE->name).

Any

Both the unknown and any type pass all data through in the form it was before.


Complex Types

Complex types build up on the primitives and add functionality around them. Following complex types are supported:

Object

Represents an Object in json with key value pairs that are known and typed. Objects are defined by an associative array in PHP, where the key needs to be a string and the value a Type or a Field.

By default, resolving a value to a field is done by checking if the key exists in the array or a property of an object.

In some cases, you might want to customize this resolution (Ex: you have a computed output field).

Using fields adds more benefits. You can describe the field or even deprecate it. This is especially useful for output types that serialize your Data.

Extending

You can define your own custom types, by implementing the Type interface. Two methods need to be implemented:

The definition method should define the Json Schema for input and output, whereas the parse function should parse the given value into the correct type. It MUST NOT throw any exception. In case of failure, Value::INVALID should be returned and all issues added to the context.

If your type needs to serialize values in a specific way you have to additionally implement the SerializesOutputValue interface.

Remember, the types should be immutable.

Following utilities (traits) are available for types to use:

Example


All versions of typescript-schema with dependencies

PHP Build Version
Package Version
Requires php Version ^8.3
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package le0daniel/typescript-schema contains the following files

Loading the files please wait ....