const Ajv = require ( 'ajv' ) ; import { ValidationContext } from '../types' ; import { validate , getType } from '../validator' ; import { pick , patch } from '../picker' ; import { compile } from '../compiler' ; import { generateJsonSchemaObject } from '../codegen' ; import { serialize , deserialize } from '../serializer' ; import * as op from '../operators' ; describe ( "foo" , function ( ) { it ( "foo-1" , function ( ) { try { const schema2 = compile ( `
                import * as fs from 'fs';
                external U, W;
                /** doc comment X - line 1
                 * doc comment X - line 2
                 */
                export type X = string | number | 'foo';
                type XX = number;
                /** doc comment S */
                enum S {
                    /** doc comment S.q */
                    q,
                    w = 3,
                    e,
                    r,
                    t = 10,
                    u = 'aaa',
                }
                export enum R {}
                interface B1111 {}
                /** doc comment B - line 1 */
                interface B {
                    @range(2, 5)
                    a: number;
                    /** doc comment B.b - line 1
                     * doc comment B.b - line 2
                     */
                    b?: string;
                }
                export interface C {
                    c?: string[10..20][];
                    d: string|number|boolean[1][2..3]|B|B[]|B|{/** doc comment C.d.p */p: string};
                    d2?: X;
                    d3?: U;
                    d4?: S;
                    [propName: /^[B][0-9]$/]: U;
                }
                interface A extends B,C, D,DD {
                    e: (string[])|(@minValue(3) number[])|(boolean[]);
                    f: X | boolean;
                    @match(/^[A-F]+$/)
                    g: string;
                    @msg('custom msg of A.h')
                    h?: C;
                    i: [string, number?, string?];
                    j: [...<number, 1..2>, string];
                    j2: [string, ...<number, 2..3>];
                    j3: string[..2];
                    k: Y;
                    l: B;
                    [propName: /^[a-z][0-9]$/ | number]?: string;
                    /** doc comment A.propName (C) */
                    [propName: /^[A][0-9]$/]: C;
                }
                interface AA extends A {}
                type Y = boolean;
                interface D {
                    d90: string;
                }
                interface DD {
                    d91: string;
                }

                interface H {
                // interface H extends HH { // <- recursive extend
                    a?: HH;
                    b: H | number;
                }
                interface HH extends H {c?:number;}

                /*
                interface HH extends H {c?:number;}
                interface H {
                    a?: HH;
                    // a?: H;
                    b: H | number;
                }
                */
            ` ) ; } catch ( e ) { throw e ; } expect ( 1 ) . toEqual ( 1 ) ; } ) ; it ( "foo-2" , function ( ) { const z = compile ( `
            type Foo = string;
            interface A {
                a: Foo;
                @range(3, 5)
                b: number;
                @maxLength(3)
                c: Foo;
            }
        ` ) ; const ctx = { checkAll : true } ; const r = validate ( { b : 6 , c : '1234' } , getType ( z , 'A' ) , ctx ) ; expect ( 1 ) . toEqual ( 1 ) ; } ) ; it ( "foo-2b" , function ( ) { const z = compile ( `
            type Foo = string;
            interface A {
                a: Foo;
                @range(3, 5)
                b: number;
                @maxLength(3)
                c: Foo;
            }
        ` ) ; const ctx = { checkAll : true } ; const subtype = op . picked ( getType ( z , 'A' ) , 'a' , 'c' ) ; const original = { a : '' , b : 3 , c : '123' , d : 0 } ; const needle = pick ( original , subtype , ctx ) ; needle . a = '1' ; needle . b = 6 ; ( needle as any ) . e = 5 ; try { const r = patch ( original , needle , subtype , ctx ) ; expect ( 1 ) . toEqual ( 1 ) ; } catch ( e ) { expect ( 1 ) . toEqual ( 0 ) ; } } ) ; it ( "foo-4" , function ( ) { const z = compile ( `
            type Foo = string;
            interface A {
                a: Foo;
                @range(3, 5)
                b: number;
                @maxLength(3)
                c: Foo;
            }
        ` ) ; const zz = deserialize ( serialize ( z ) ) ; expect ( 1 ) . toEqual ( 1 ) ; } ) ; it ( "readme-examples-1" , function ( ) { { const mySchema = compile ( `
                type Foo = string;
                interface A {
                    @maxLength(4)
                    a: Foo;
                    z?: boolean;
                }
            ` ) ; type Foo = string ; interface A { a : Foo ; z ? : boolean ; } { const validated1 = validate ( { a : 'x' , b : 3 , } , getType ( mySchema , 'A' ) ) ; expect ( validated1 ) . toEqual ( { value : { a : 'x' , b : 3 } } ) ; const validated2 = validate ( { aa : 'x' , b : 3 , } , getType ( mySchema , 'A' ) ) ; if ( validated2 === null ) { expect ( 1 ) . toEqual ( 1 ) ; } else { expect ( 1 ) . toEqual ( 0 ) ; } const ctx3 = { checkAll : true , noAdditionalProps : true , schema : mySchema , } ; const validated3 = validate ( { aa : 'x' , b : 3 , } , getType ( mySchema , 'A' ) , ctx3 ) ; if ( validated3 === null ) { expect ( 1 ) . toEqual ( 1 ) ; } else { expect ( 1 ) . toEqual ( 0 ) ; } } { const original = { a : 'x' , b : 3 , } ; const needleType = op . picked ( getType ( mySchema , 'A' ) , 'a' ) ; try { const needle1 = pick ( original , needleType ) ; const unknownInput1 = { ... needle1 , a : 'y' , q : 1234 , } ; const changed1 = patch ( original , unknownInput1 , needleType ) ; expect ( changed1 ) . toEqual ( { a : 'y' , b : 3 } ) ; } catch ( e ) { expect ( 1 ) . toEqual ( 0 ) ; } try { const needle2 = pick ( original , needleType ) ; const unknownInput2 = { ... needle2 , a : 'yyyyy' , q : 1234 , } ; const changed1 = patch ( original , unknownInput2 , needleType ) ; expect ( 1 ) . toEqual ( 0 ) ; } catch ( e ) { expect ( 1 ) . toEqual ( 1 ) ; } try { const ctx3 = { checkAll : true , schema : mySchema , } ; const needle3 = pick ( { aa : 'x' , b : 3 , } , needleType , ctx3 ) ; expect ( 1 ) . toEqual ( 0 ) ; } catch ( e ) { expect ( 1 ) . toEqual ( 1 ) ; } } { const unknownInput = { a : 'x' } ; const validated = validate < A > ( unknownInput , getType ( mySchema , 'A' ) ) ; if ( validated ) { const validatedInput = validated . value ; expect ( validatedInput ) . toEqual ( { a : 'x' } ) ; } else { expect ( 1 ) . toEqual ( 0 ) ; } } { interface Store { baz : A ; } const store = { baz : { a : 'x' , z : false , } } ; const needleType = op . picked ( getType ( mySchema , 'A' ) , 'a' ) ; try { const needle = pick ( store . baz , needleType ) ; const unknownInput = { ... needle , a : 'y' , q : 1234 , } ; store . baz = patch ( store . baz , unknownInput , needleType ) ; expect ( store . baz ) . toEqual ( { a : 'y' , z : false } ) ; } catch ( e ) { expect ( 1 ) . toEqual ( 0 ) ; } } } } ) ; } ) ;