ts.data.json - v4.1.0
    Preparing search index...

    Function objectStrict

    • Decoder for objects with specified field decoders that fails if unknown fields are present.

      Type Parameters

      • T

      Parameters

      Returns Decoder<T>

      A decoder that validates and returns objects matching the specified structure, failing if unknown fields are present

      interface User {
      name: string;
      age: number;
      }

      const userDecoder = JsonDecoder.objectStrict<User>({
      name: JsonDecoder.string(),
      age: JsonDecoder.number()
      });

      userDecoder.decode({name: 'John', age: 30}); // Ok<User>
      userDecoder.decode({name: 'John', age: 30, extra: 'field'}); // Err with issues: [{ message: 'Unknown key "extra" found in strict object', path: [] }]
      // Use the fromKey API to decode from different JSON keys
      const userDecoder = JsonDecoder.objectStrict<User>({
      name: { fromKey: 'user_name', decoder: JsonDecoder.string() },
      age: JsonDecoder.number()
      });

      userDecoder.decode({user_name: 'John', age: 30}); // Ok<User>